Jul 21, 2015

Shallow Copy and Deep Copy Using C#

When we copy one instance to another using C# what happen is that both instances share the same memory address. But this is not the behavior we want most of the time.

When we create a copy of an object, for example:

MyClass obj=new MyClass()MyClass obj2=obj;
Then the '=' operator copies the reference and not the object (and it works fine for a Value Type).

By default we get this behavior using the MemberwiseClone() method that is defined in the super class called System.Object. This is called “Shallow Copy”.

To get the same behavior for a Reference Type as well as a Value Type we use the Clone() method that belongs to the System.ICloneable interface. This is called a “Deep Copy”.

We will see both behaviors in depth one by one.




Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements.
Deep copies duplicate everything. A deep copy of a collection is two collections with all of the elements in the original collection duplicated.
 
If B is a shallow copy of A, then it is like B = [A assign];
B and A point to the same memory location
If B is a deep copy of A, then it is like B = [A copy];
B and A point to different memory locations
B memory address is same as A's
B has same contents as A's


char * Source = "Hello, world.";

char * ShallowCopy = Source; 

char * DeepCopy = new char(strlen(Source)+1);
strcpy(DeepCopy,Source);
 

char * Source = "Hello, world.";

char * ShallowCopy = Source; 

char * DeepCopy = new char(strlen(Source)+1);
strcpy(DeepCopy,Source);
 
'ShallowCopy' points to the same location in memory as 'Source' does. 'DeepCopy' points to a different location in memory, but the contents are the same.

Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements.
Deep copies duplicate everything. A deep copy of a collection is two collections with all of the elements in the original collection duplicated.


Your example is creating a shallow copy.
A ob1 = new A();
ob1.a = 10;
A ob2 = new A();
ob2 = ob1;

ob1.a = 5; // <-- If you see value of ob2.a after this line, it will be 5.
Deep copy will be -
 A ob1 = new A();
 ob1.a = 10;
 A ob2 = new A();
 ob2.a = ob1.a;

 ob1.a = 5; // <-- If you see value of ob2.a after this line, it will be 10.
 What is Shallow copy ?
 
 
 


 
Shallow copying is creating a new object and then copying the non static fields of the current object to the new object. If the field is a value type, a bit by bit copy of the field is performed. If the field is a reference type, the reference is copied but the referred object is not, therefore the original object and its clone refer to the same object. A shallow copy of an object is a new object whose instance variables are identical to the old object. In .Net shallow copy is done by the object method MemberwiseClone().
The situations like , if you have an object with values and you want to create a copy of that object in another variable from same type, then you can use shallow copy, all property values which are of value types will be copied, but if you have a property which is of reference type then this instance will not be copied, instead you will have a reference to that instance only.

What is Deep copy ?

 

Deep copy is creating a new object and then copying the non-static fields of the current object to the new object. If a field is a value type, a bit by bit copy of the field is performed. If a field is a reference type, a new copy of the referred object is performed. A deep copy of an object is a new object with entirely new instance variables, it does not share objects with the old. While performing Deep Copy the classes to be cloned must be flagged as [Serializable].
Deep copy is intended to copy all the elements of an object, which include directly referenced elements of value type and the indirectly referenced elements of a reference type that holds a reference to a memory location that contains data rather than containing the data itself.

 
 
 

No comments:

Post a Comment