A shallow copy creates a new instance of the same type as the original object, and then copies the non-static fields of the original object.
The MemberwiseClone method creates a shallow copy by creating a new object, and then copying the non static fields of the current object to the new object. If the field is a value type, a bit-by-bit copy of the field is performed. If the field is a reference type, the reference is copied but the referred object is not;
Simply saying, shallow copy copies the structure of the object, deep copy copies structure as well as data
In a deep copy, all objects are duplicated; whereas, in a shallow copy, only the top-level objects are duplicated and the lower levels contain references..
For example, consider an object called 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 C. Use a class that implements the ICloneable interface to perform a deep or shallow copy of an object.
For example, consider an object called 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 C. Use a class that implements the ICloneable interface to perform a deep or shallow copy of an object.
The following program will explain ...
namespace shallowcopyingexpl { public class IdInfo { public int IdNumber; public IdInfo(int IdNumber) { this.IdNumber = IdNumber; } } public class Person { public int Age; public string Name; public IdInfo IdInfo; public Person ShallowCopy() { return (Person)this.MemberwiseClone(); } public Person DeepCopy() { Person other = (Person)this.MemberwiseClone(); other.IdInfo = new IdInfo(this.IdInfo.IdNumber); return other; } } public class Example { public static void Main() { // Create an instance of Person and assign values to its fields. Person p1 = new Person(); p1.Age = 42; p1.Name = "Sam"; p1.IdInfo = new IdInfo(6565); // Perform a shallow copy of p1 and assign it to p2. Person p2 = (Person)p1.ShallowCopy(); // Display values of p1, p2 Console.WriteLine("Original values of p1 and p2:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values:"); DisplayValues(p2); // Change the value of p1 properties and display the values of p1 and p2. p1.Age = 32; p1.Name = "Frank"; p1.IdInfo.IdNumber = 7878; Console.WriteLine("\nValues of p1 and p2 after changes to p1:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values:"); DisplayValues(p2); // Make a deep copy of p1 and assign it to p3. Person p3 = p1.DeepCopy(); // Change the members of the p1 class to new values to show the deep copy. p1.Name = "George"; p1.Age = 39; p1.IdInfo.IdNumber = 8641; Console.WriteLine("\nValues of p1 and p3 after changes to p1:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p3 instance values:"); DisplayValues(p3); Console.Read(); } public static void DisplayValues(Person p) { Console.WriteLine(" Name: {0:s}, Age: {1:d}", p.Name, p.Age); Console.WriteLine(" Value: {0:d}", p.IdInfo.IdNumber); } } } //changes in one object doesn't reflect the other object .just structure only copied from one object to other not data. Output: Original values of p1 and p2: p1 instance values: Name: Sam, Age: 42 Value: 6565 p2 instance values: Name: Sam, Age: 42 Value: 6565 Values of p1 and p2 after changes to p1: p1 instance values: Name: Frank, Age: 32 Value: 7878 p2 instance values: Name: Sam, Age: 42 Value: 7878 Values of p1 and p3 after changes to p1: p1 instance values: Name: George, Age: 39 Value: 8641 p3 instance values: Name: Frank, Age: 32 Value: 7878 |
No comments:
Post a Comment