Introduction
Each members of the instance will be copied according to their type(value type or reference type). If one member of the instance is a value type, the shallow copy will copy the value in a new variable.BUT if the instance member is a reference type, we will have a copy of the reference and not a copy of the value of this member.
In a shallow cloning a new instance of the type is created and the values are copied into the new instance. The reference pointers are also copied just like the values. Therefore, the references are pointing to the original objects. Any changes to the members that are stored by reference appear in both the original and the copy, since no copy was made of the referenced object.
In a deep cloning, the fields that are stored by value are copied as before, but the pointers to objects stored by reference are not copied. Instead, a deep copy is made of the referenced object, and a pointer to the new object is stored. Any changes that are made to those referenced objects will not affect other copies of the object.
Example
class CompanyPhone
{
public int Phone=100;
}
Class CompanyBase:CompanyPhone
{
public static string CompanyName = "My Company";
public int age;
public string name;
}
class CompanyDErivedClass : CompanyBase
{
static void Main()
{
// Creates an instance of MyDerivedClass and assign values to its fields.
CompanyDErivedClass m1 = new CompanyDErivedClass();
m1.age = 42;
m1.name="Sant";m1.Phone = 9999;
Console.WriteLine(m1.age);
Console.WriteLine(m1.name);
Console.WriteLine("Original Phone:" + m1.Phone.ToString());
// Performs a shallow copy/Clone of m1 and assign it to m2.
CompanyDErivedClass m2 = (CompanyDErivedClass)m1.MemberwiseClone();
m2.Phone = 101;Console.WriteLine(m2.age);
Console.WriteLine(m2.name);
Console.WriteLine("Shallow Cloned Phone"+m2.Phone.ToString());
Console.WriteLine("Original Phone:" + m1.Phone.ToString());
//Perform Deep Copy/Clone
CompanyDErivedClass m3 = m1;
m3.Phone = 102;Console.WriteLine(m3.age);
Console.WriteLine(m3.name);
Console.WriteLine("Deep Clone phone" + m3.Phone.ToString());
Console.WriteLine("Original Phone:" + m1.Phone.ToString());
Console.ReadKey();
}
}
OutPut:
{
public int Phone=100;
}
Class CompanyBase:CompanyPhone
{
public static string CompanyName = "My Company";
public int age;
public string name;
}
class CompanyDErivedClass : CompanyBase
{
static void Main()
{
// Creates an instance of MyDerivedClass and assign values to its fields.
CompanyDErivedClass m1 = new CompanyDErivedClass();
m1.age = 42;
m1.name="Sant";m1.Phone = 9999;
Console.WriteLine(m1.age);
Console.WriteLine(m1.name);
Console.WriteLine("Original Phone:" + m1.Phone.ToString());
// Performs a shallow copy/Clone of m1 and assign it to m2.
CompanyDErivedClass m2 = (CompanyDErivedClass)m1.MemberwiseClone();
m2.Phone = 101;Console.WriteLine(m2.age);
Console.WriteLine(m2.name);
Console.WriteLine("Shallow Cloned Phone"+m2.Phone.ToString());
Console.WriteLine("Original Phone:" + m1.Phone.ToString());
//Perform Deep Copy/Clone
CompanyDErivedClass m3 = m1;
m3.Phone = 102;Console.WriteLine(m3.age);
Console.WriteLine(m3.name);
Console.WriteLine("Deep Clone phone" + m3.Phone.ToString());
Console.WriteLine("Original Phone:" + m1.Phone.ToString());
Console.ReadKey();
}
}
OutPut:
While looking the out put What ever value is modified in cloned object it is not modified Original object.But in the modification which we have done in Deep Cloned object it directly reflect in the Original Objet.
No comments:
Post a Comment