Articles → CSHARP → Deep Copy And Shallow CopyDeep Copy And Shallow CopyLet us elaborate it with an exampleShallow Copy I am declaring 2 arraylistArrayList a = new ArrayList(); ArrayList b = new ArrayList();Now I have added an element in the arraylista.Add("2");Now use the clone methodb = (ArrayList)a.Clone(); // shallow copyNow if you see no of elements in b, it will show 1and no of elements in a is also 1 i.e. "2"Now add another element in an arraylist aa.Add("3");Now if you see no of elements in b, it will show 1and no of elements in a is also 2 i.e. "2" and "3". shallow copy allows you to replicate the arraylist once. it then ignores the relationship it has with the original array.Deep Copy Now consider the example aboveinstead of using clone method useb = a;Now if you see no of elements in b, it will show 1and no of elements in a is also 1 i.e. "2"Now add another element in an arraylist aa.Add("3");Now if you see no of elements in b, it will show 2and no of elements in a is also 2 i.e. "2" and "3".The deep copy will be persistent and any changes you make to the original arraylist will be reflected in both copies.Posted By - Karan Gupta Posted On - Wednesday, May 5, 2010 Query/Feedback Your Email Id** Subject* Query/Feedback Characters remaining 250**
ArrayList a = new ArrayList(); ArrayList b = new ArrayList();
a.Add("2");
b = (ArrayList)a.Clone(); // shallow copy
a.Add("3");
b = a;
Query/Feedback