Cloning Of Object, Shallow Copy And Deep Copy In C#

In this article, I am going to share with you about cloning of objects, shallow copy, and deep copy in C#:
 
What is Cloning?
 
It's creating a new object by copying the current instance/object.
 
Cloning can be implemented in two ways,
  • Shallow copy
  • Deep copy
In deep copy, all objects are duplicated, while in Shallow copy only top-level objects are duplicated and other lower level objects are referenced.
 
For example, consider an object 'X' that references objects 'A' and 'B'. Object 'B', in turn, references object 'C'. A shallow copy of 'X' creates new object 'X2' that also references objects 'A' and 'B'. In contrast, a deep copy of 'X' creates a new object 'X2' that references the new objects 'A2' and 'B2', which are copies of 'A' and 'B'. 'B2', in turn, references the new object 'C2', which is a copy of 'C'. The example shows the difference between a shallow and a deep copy.

Shallow Copy
 
'MemberwiseClone' method is used to create a shallow copy of current object/instance.
 
It creates a shallow copy by creating a new object and then copying the non-static members of the current object to the new object. For value type members, the bit-by-bit copy of the members is performed while for reference type members, the reference is copied but the referred object is not. So, the original object and its clone both refer to the same object.
 
Look at the below program to understand the shallow copy practically.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.IO;  
  5. using System.Linq;  
  6. class Solution  
  7. {  
  8.     public class Course  
  9.     {  
  10.         public int Course_Id;  
  11.   
  12.         // Constructor  
  13.         public Course(int Id)  
  14.         {  
  15.             this.Course_Id = Id;  
  16.         }  
  17.     }  
  18.   
  19.     public class Student  
  20.     {  
  21.         public int Roll_No;  
  22.         public string Name;  
  23.         public Course Course_ID;  
  24.   
  25.         // Method that perform shallow copy  
  26.         public Student ShallowCopy()  
  27.         {  
  28.             return (Student)this.MemberwiseClone();  
  29.         }  
  30.     }  
  31.     static void Main(String[] args)  
  32.     {  
  33.         // Create an instance of Student and assign values to its fields.  
  34.         Student s = new Student();  
  35.   
  36.         s.Name = "Jaimy lons";  
  37.         s.Roll_No = 2;  
  38.         s.Course_ID = new Course(65);  
  39.   
  40.         // Perform a shallow copy of 's' and assign it to 's2'.  
  41.         Student s2 = s.ShallowCopy();  
  42.   
  43.         // Display values of s, s2  
  44.         Console.WriteLine("value of object 's'");  
  45.         Console.WriteLine("Name: " + s.Name);  
  46.         Console.WriteLine("Roll No.: " + s.Roll_No);  
  47.         Console.WriteLine("Course ID: " + s.Course_ID.Course_Id);  
  48.         Console.WriteLine();  
  49.         Console.WriteLine("value of object 's1'");  
  50.         Console.WriteLine("Name: " + s2.Name);  
  51.         Console.WriteLine("Roll No.: " + s2.Roll_No);  
  52.         Console.WriteLine("Course ID: " + s2.Course_ID.Course_Id);  
  53.         Console.WriteLine();  
  54.   
  55.         // modifying values of object 's'  
  56.         s.Name = "Taylor peter";  
  57.         s.Roll_No = 38;  
  58.         s.Course_ID.Course_Id = 6;  
  59.   
  60.         // Display values of 's', 's2'  
  61.         Console.WriteLine("value of object 's'");  
  62.         Console.WriteLine("Name: " + s.Name);  
  63.         Console.WriteLine("Roll No.: " + s.Roll_No);  
  64.         Console.WriteLine("Course ID: " + s.Course_ID.Course_Id);  
  65.         Console.WriteLine();  
  66.         Console.WriteLine("value of object 's1'");  
  67.         Console.WriteLine("Name: " + s2.Name);  
  68.         Console.WriteLine("Roll No.: " + s2.Roll_No);  
  69.         Console.WriteLine("Course ID: " + s2.Course_ID.Course_Id);  
  70.   
  71.         Console.ReadLine();  
  72.     }  
  73. }  
Output
  1. value of object 's'  
  2. Name: Jaimy lons  
  3. Roll No.: 2  
  4. Course ID: 65  
  5.   
  6. value of object 's1'  
  7. Name: Jaimy lons  
  8. Roll No.: 2  
  9. Course ID: 65  
  10.   
  11. value of object 's'  
  12. Name: Taylor peter  
  13. Roll No.: 38  
  14. Course ID: 6  
  15.   
  16. value of object 's1'  
  17. Name: Jaimy lons  
  18. Roll No.: 2  
  19. Course ID: 6  
As we can see in the output, after changing the value of object 's', the value of reference type gets changed to object 's2'(Course ID). Value type member's values remain the same because they got copied bit-by-bit, while reference type members are not copied but referenced.
 
Deep Copy
 
Below are the ways to implement a deep copy operation.

Call the MemberwiseClone method to create a shallow copy of an object, and then assign new objects whose values are the same as the original object to any properties or fields whose values are reference types.

Look at the below program to understand the deep copy practically.
 
Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.IO;  
  5. using System.Linq;  
  6. class Solution  
  7. {  
  8.     public class Course  
  9.     {  
  10.         public int Course_Id;  
  11.   
  12.         // Constructor  
  13.         public Course(int Id)  
  14.         {  
  15.             this.Course_Id = Id;  
  16.         }  
  17.     }  
  18.   
  19.     public class Student  
  20.     {  
  21.         public int Roll_No;  
  22.         public string Name;  
  23.         public Course Course_ID;  
  24.   
  25.         // Method that perform deep copy  
  26.         public Student DeepCopy()  
  27.         {  
  28.             Student other = (Student)this.MemberwiseClone();  
  29.             other.Course_ID = new Course(Course_ID.Course_Id);  
  30.             other.Name = String.Copy(Name);  
  31.             return other;  
  32.         }  
  33.     }  
  34.     static void Main(String[] args)  
  35.     {  
  36.         // Create an instance of Student and assign values to its fields.  
  37.         Student s = new Student();  
  38.   
  39.         s.Name = "Jaimy lons";  
  40.         s.Roll_No = 2;  
  41.         s.Course_ID = new Course(65);  
  42.   
  43.         // Perform a shallow copy of 's' and assign it to 's2'.  
  44.         Student s2 = s.DeepCopy();  
  45.   
  46.         // Display values of s, s2  
  47.         Console.WriteLine("value of object 's'");  
  48.         Console.WriteLine("Name: " + s.Name);  
  49.         Console.WriteLine("Roll No.: " + s.Roll_No);  
  50.         Console.WriteLine("Course ID: " + s.Course_ID.Course_Id);  
  51.         Console.WriteLine();  
  52.         Console.WriteLine("value of object 's1'");  
  53.         Console.WriteLine("Name: " + s2.Name);  
  54.         Console.WriteLine("Roll No.: " + s2.Roll_No);  
  55.         Console.WriteLine("Course ID: " + s2.Course_ID.Course_Id);  
  56.         Console.WriteLine();  
  57.   
  58.         // modifying values of object 's'  
  59.         s.Name = "Taylor peter";  
  60.         s.Roll_No = 38;  
  61.         s.Course_ID.Course_Id = 6;  
  62.   
  63.         // Display values of 's', 's2'  
  64.         Console.WriteLine("value of object 's'");  
  65.         Console.WriteLine("Name: " + s.Name);  
  66.         Console.WriteLine("Roll No.: " + s.Roll_No);  
  67.         Console.WriteLine("Course ID: " + s.Course_ID.Course_Id);  
  68.         Console.WriteLine();  
  69.         Console.WriteLine("value of object 's1'");  
  70.         Console.WriteLine("Name: " + s2.Name);  
  71.         Console.WriteLine("Roll No.: " + s2.Roll_No);  
  72.         Console.WriteLine("Course ID: " + s2.Course_ID.Course_Id);  
  73.   
  74.         Console.ReadLine();  
  75.     }  
  76. }  
Output
  1. value of object 's'  
  2. Name: Jaimy lons  
  3. Roll No.: 2  
  4. Course ID: 65  
  5.   
  6. the value of object 's1'  
  7. Name: Jaimy lons  
  8. Roll No.: 2  
  9. Course ID: 65  
  10.   
  11. value of object 's'  
  12. Name: Taylor peter  
  13. Roll No.: 38  
  14. Course ID: 6  
  15.   
  16. value of object 's1'  
  17. Name: Jaimy lons  
  18. Roll No.: 2  
  19. Course ID: 65  
As we can see in the output, after changing the value of object 's', the value of reference type remains the same for object 's2'(Course ID).This is called deep copy.
 
Call a class constructor of the object to be copied to create a second object with property values taken from the first object. This assumes that the values of an object are entirely defined by its class constructor.
 
Serialize the object to be deep copied, and then restore the serialized data to a different object variable.
 
Use reflection with recursion to perform the deep copy operation.
 
Reference
 
https://msdn.microsoft.com/en-us/library/system.object.memberwiseclone%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
 
Conclusion
 
Shallow copy and deep copy are two different ways used for cloning objects. If we talk about time consumption, deep copy consumes more time than a shallow copy. I hope this article helps you to understand a bit more about the cloning of objects, shallow copy and deep copy.
 
Thank you. Please feel free to ask any question or make a suggestion. You can read my other articles or blogs on this link. C# Articles