Creating a new object by copying the current instance/object is called object cloning. It’s important for a beginner programmer to understand, especially the programmers who are coming from the other programming languages because this could be quite confusing for them.
For example, if you are a C++ programmer and you are new to C# then this is to understand that in C++, by default the assignment operator is a shallow copy. But in C#, the assignment operator is not a shallow copy. It’s similar and if you didn’t understand the differences, you would almost always assume that it is performing a shallow copy but it’s slightly different.
There are two types of cloning ways in C#.
They both involve copying data from one variable to another for one object to another.
In a shallow copy, a new object can be created from the existing object and it will copy the value type fields to the new object. But for the reference type fields, it will only copy the reference. Means the memory location for both the objects is the same. So if the value of the reference type field is changed for any object, it will be affected / reflected on the other one.
Let;s take an example.
Here class Employee has two public properties, string Department and EmployeeDetails Details. And this EmployeeDetails is just their firstname and lastname. The reason why I’ve split it up like this is because I want to be able to have a reference type here so that the differences can be clearly seen during performing the clone.
Also, there is a method called ShallowCopy which returns the type of object and the reason why I’m returning type object is because there is a built-in to the object class called MemberwiseClone() and if I highlight it, it shows the details saying “Creates a shallow copy of the current object”. See the below image.
Now let’s see the result of executing the shallow copy.
Here the value of e1 is just dumped into e2. Now if I want to change the value of e2, let’s see what happens.
Both are showing the same first name. This is happening because the memory location for both are same so changing the value of any of them will change the value of another one.
In deep copy, a new object can be created from the existing object and it will copy the fields to the new object. And the point for deep copy is, field type does not matter. whether it is value type or reference type, it always makes a copy of whole data and stores it in a different memory location. So if any field value is changed for any object, it won't affect the other object.
Let’s understand with the same above example but instead of the ShallowCopy, the DeepCopy method is used.
Now let’s see the result of executing the deep copy.
Now I want to change the value of e2.
Here both the names are different because the memory location for both are different and hence changing the value of one variable is not affecting the other.
So the difference between Shallow copy and Deep copy is,
A shallow copy, if it’s a value type data it’s just going to copy the data over but if it’s a reference type, it’s just going to copy the reference or their actual memory location and that will be copied over so that they both point to the same location.
A deep copy, if it’s a value type data it’s just going to copy the data over just like the shallow copy. But if it’s a reference type, it creates a new memory location and then copies the data over. That’s why if the value of any variable is changed, it won’t change the value of others.
Conclusion: Deep copy is recommended to implement because It allows us to modify the clone object without affecting the existing object.