Introduction
C# is pronounced C sharp. It is a very famous programming language built by Microsoft and operates on the .Net framework. It is closely related to the C programming language, as well as other programming languages such as C++ and Java. Its most impressive feature is its reusability. The language is easy to learn and understand. In the Prototype Design Pattern, the objects are copied using both shallow and deep copies. So, in this blog, we'll look at what Shadow Copy and Deep Copy are, as well as the differences between them.
Recommended Topic, Palindrome in C#, singleton design pattern in c# and Ienumerable vs Iqueryable
Shallow Copy
Shallow copying entails creating a new object and then copying the non-static fields of the existing object to the new one. If the field is of type value, a bit-by-bit duplication is performed. The original object and its clone refer to the same object since the reference is copied, but the referred object is not. The shallow copy of an object is a new object that shares the original's instance variables. Shallow copying is performed by the .Net object method MemberwiseClone().
The shallow copy can be used if you have an object with values and wish to duplicate it in another variable of the same type. All property values of value kinds will be replicated, but you will only have a reference to that instance if you have a property of reference type.
Example
The shallow copy creates a new object from an existing one and then replicates the old object's value type fields to the new one. In contrast, the reference type will only copy the reference, not the referred object. The original and clone relate to the same item in the case of reference type. Consider the following code snippet.
Code:
using System;
namespace PrototypeDesignPattern
{
class Program
{
static void Main(string[] args)
{
Student s1 = new Student();
s1.Name = "John"; //assigning value
s1.Department = "CSE"; //assigning value
s1.StudAddress = new Address() { address = "Delhi"};
Student s2 = s1.GetClone();
s2.Name = "Jenny"; //assigning value
s2.StudAddress.address = "Mumbai"; //assigning value
Console.WriteLine("Student 1: ");
Console.WriteLine("Name: " + s1.Name + ", Address: " + s1.StudAddress.address + ", Dept: " + s1.Department);
Console.WriteLine("Student 2: ");
Console.WriteLine("Name: " + s2.Name + ", Address: " + s2.StudAddress.address + ", Dept: " + s2.Department);
Console.Read();
}
}
public class Student
{
public string Name { get; set; }
public string Department { get; set; }
public Address StudAddress { get; set; }
public Student GetClone()
{
return (Student)this.MemberwiseClone();
}
}
public class Address
{
public string address { get; set; }
}
}
Output:
Student 1:
Name: John, Address: Mumbai, Dept: CSE
Student 2:
Name: Jenny, Address: Mumbai, Dept: CSE
Explanation:
We update the address and name of the second student, s2, in the above code after establishing the second Student object. It is observed that the address for the s1 has also been modified.