Shallow Copy and Deep Copy in C#

Shallow Copy and Deep Copy in C# with Examples

In this article, I am going to discuss the Shallow Copy and Deep Copy in C# with examples. This is a continuation part of our previous article. So, please read our previous article where we discussed the Prototype Design Pattern in C# with examples before proceeding to this article.

Shallow Copy and Deep Copy play important role in copying the objects in Prototype Design Pattern. So, in this article, we are going to discuss what are Shallow Copy and Deep Copy and the differences between them with examples.

Understanding Shallow Copy in C#:

In the case of Shallow copy, it will create the new object from the existing object and then copying the value type fields of the current object to the new object. But in the case of reference type, it will only copy the reference, not the referred object itself. Therefore the original and clone refer to the same object in case of reference type. In order to understand this better, please have a look at the following diagram.

understanding Shallow Copy in C# with example

As shown in the above diagram, first we create an object i.e. emp1 and then initialize the object with some values. Then we create the second object i.e. emp2 using the GetClone method. As shown in the memory representation, the value type fields (Name and Department) are copied and stored in a different memory location while the reference type field i.e. EmpAddress is still pointing to the same old memory location. That means now, both the object i.e. emp1 and emp2 is now referring to the same Address object. So, if we do any changes to the employee address then it will affect each other.

The complete code is given below.
using System;
namespace PrototypeDesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee emp1 = new Employee();
            emp1.Name = "Anurag";
            emp1.Department = "IT";
            emp1.EmpAddress = new Address() { address = "BBSR"};

            Employee emp2 = emp1.GetClone();
            emp2.Name = "Pranaya";
            emp2.EmpAddress.address = "Mumbai";

            Console.WriteLine("Emplpyee 1: ");
            Console.WriteLine("Name: " + emp1.Name + ", Address: " + emp1.EmpAddress.address + ", Dept: " + emp1.Department);
            Console.WriteLine("Emplpyee 2: ");
            Console.WriteLine("Name: " + emp2.Name + ", Address: " + emp2.EmpAddress.address + ", Dept: " + emp2.Department);

            Console.Read();
        }
    }

    public class Employee
    {
        public string Name { get; set; }
        public string Department { get; set; }
        public Address EmpAddress { get; set; }

        public Employee GetClone()
        {
            return (Employee)this.MemberwiseClone();
        }
    }

    public class Address
    {
        public string address { get; set; }
    }
}

In the above code, after creating the second employee object, we change the address and name of the second employee i.e. emp2. Now when you run the application, you will see that the address is changed for the emp1 also as shown in the below image.

Shallow Copy and Deep Copy in C#

Understanding Deep Copy in C#:

In the case of deep copy, it will create the new object from the existing object and then copying the fields of the current object to the newly created object. If the field is a value type, then a bit-by-bit copy of the field will be performed. If the field is a reference type, then a new copy of the referred object is created.

Deep Copy in C#

As shown in the above image, the Name and Department properties are value type so it creates a copy of that and stores it in a different location. The EmpAddress is a Reference type property and in Deep Copy there is a clone of reference type field which also will be stored in a different location. So, the point that you need to keep in mind is, in the case of Deep Copy the field type does not matter whether it is a value type or reference type. It always makes a copy of the whole data and stores it in a different memory location. 

The complete code is given below.
using System;
namespace PrototypeDesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee emp1 = new Employee();
            emp1.Name = "Anurag";
            emp1.Department = "IT";
            emp1.EmpAddress = new Address() { address = "BBSR"};

            Employee emp2 = emp1.GetClone();
            emp2.Name = "Pranaya";
            emp2.EmpAddress.address = "Mumbai";

            Console.WriteLine("Emplpyee 1: ");
            Console.WriteLine("Name: " + emp1.Name + ", Address: " + emp1.EmpAddress.address + ", Dept: " + emp1.Department);
            Console.WriteLine("Emplpyee 2: ");
            Console.WriteLine("Name: " + emp2.Name + ", Address: " + emp2.EmpAddress.address + ", Dept: " + emp2.Department);

            Console.Read();
        }
    }

    public class Employee
    {
        public string Name { get; set; }
        public string Department { get; set; }
        public Address EmpAddress { get; set; }

        public Employee GetClone()
        {
            Employee employee = (Employee)this.MemberwiseClone();
            employee.EmpAddress = EmpAddress.GetClone();
            return employee;
        }
    }

    public class Address
    {
        public string address { get; set; }
        public Address GetClone()
        {
            return (Address)this.MemberwiseClone();
        }
    }
}

Output:

Understanding Shallow Copy and Deep Copy in C# with examples

From the next article onwards, I am going to discuss the Structural Design Patterns in C# with examples. Here, in this article, I try to explain the Shallow Copy and Deep Copy in C# step by step with some simple examples. I hope you enjoy this article.

Leave a Reply

Your email address will not be published. Required fields are marked *