Confused about shallow and deep copy in C#
-
Hi folks, I am kind of confused about C# def. about shallow copy. As far as I know, shallow copy points to the same memory location as the orginal, so if we make some changes to the copy, the orginal will be changed. I did a test by doing the followings: ArrayList a=new ArrayList(); a.Add(1); a.Add(2); ArrayList b=(ArrayList)a.Clone() /*C# says it will create a shallow copy here */ b.Add(3); Now a and b are difference, I am confused here. What am I misunderstading? And how to create a true deep and shallow copy in C#? Thanks.
-
Hi folks, I am kind of confused about C# def. about shallow copy. As far as I know, shallow copy points to the same memory location as the orginal, so if we make some changes to the copy, the orginal will be changed. I did a test by doing the followings: ArrayList a=new ArrayList(); a.Add(1); a.Add(2); ArrayList b=(ArrayList)a.Clone() /*C# says it will create a shallow copy here */ b.Add(3); Now a and b are difference, I am confused here. What am I misunderstading? And how to create a true deep and shallow copy in C#? Thanks.
First, you need to understand the difference between value types and reference types. All structures and primitives (such as int, long, float) are value types. Value types exist in the stack and are always copied. For example:
int a = 1; // Set a to 1. int b = a; // Copy a's value to b.
All classes are reference types and their objects exist in the heap. Variables used to access class objects really only store addresses (so to speak) to their objects. Keyword "new" creates an object and returns a reference to that object. For example:ArrayList a = new ArrayList(); // 'a' refers to new ArrayList object. ArrayList b = a; // b copies a's reference, but refers to same object.
Now, a shallow copy means that only an object's member variables are copied. This means the value types and references are copied, but the referenced objects themselves are not copied/cloned. A deep copy is when you clone the referenced objects too. You can do this by overriding your class' Clone() method and explicitly clone your reference member variables. In the case of your ArrayList example, it's storing integers which are value types. So, changing the cloned ArrayList's items will not effect the original ArrayList. However, if the original ArrayList stored reference types instead, then that would be another story. Phew! That was a bit long winded, but I hope that made sense. -
Hi folks, I am kind of confused about C# def. about shallow copy. As far as I know, shallow copy points to the same memory location as the orginal, so if we make some changes to the copy, the orginal will be changed. I did a test by doing the followings: ArrayList a=new ArrayList(); a.Add(1); a.Add(2); ArrayList b=(ArrayList)a.Clone() /*C# says it will create a shallow copy here */ b.Add(3); Now a and b are difference, I am confused here. What am I misunderstading? And how to create a true deep and shallow copy in C#? Thanks.
the example you shown is shallow copy. To achieved deep copy you need to create an object for arraylist and not a reference to it ArrayList b = new ArrayList(). int i = 0 while (i < b.count) { a[i] = b[i]; i++; } shallow copy mean two pointers pointing at the same piece of memory.