. Net platform-based deep copy and shallow copy

Source: Internet
Author: User

In the. net class library, object cloning is widely used in various types of implementation. All types that implement the ICloneable interface can clone their object instances. Therefore, the deep copy and shallow copy mentioned in this article are based on the implementation of the ICloneable interface. Basic concept: shallow copy: The field of the object is copied, and the object referenced by the field is not copied. The Copied object and the original object are only referenced by different names, but they share an object. Changes to any object will affect another object. Most of the reference types are implemented in the shortest copy mode. The value assignment between reference type objects is to copy a copy of the Object Reference address, and the object instance is still the same. Deep copy: the object's child segment is copied, and the object referenced by the field is also copied. Deep copy creates the structure of the entire source object. The copy object and the original object are independent of each other and do not share any instance data. Modifying an object does not affect the other object. The value assignment operation between value types performs deep copy. Reference code for basic concepts: class Program {static void Main (string [] args) {Student s1 = new Student ("li", 23 ); // copy Student s2 = s1; s2.Age = 27; s1.ShowInfo (); // li's age is 27 // deep copy int I = 12; int j = I; j = 22; Console. writeLine (I); // 12 Console. read () ;}} class Student {public string Name; public int Age; public Student (string name, int age) {Name = name; Age = age ;} public void ShowInfo () {Console. wri TeLine ("{0}'s age is {1}", Name, Age) ;}analysis: In the previous example, instance s2 made a shortest copy of s1, modify the Age field in s2, and then affect the Age field in instance s1. In deep copy, it is only a simple value assignment between value types. Changes made to "j" do not change the value of "I. Implementation of deep copy: public object Clone () {return this. MemberwiseClone ();} MemberwiseClone: Create a superficial copy. The process is to create a new object, and then copy the non-static fields of the current object to the new object. If the field is of the value type, perform a one-on-one copy of the field. If the field is of the reference type, copy the reference but do not copy the reference object. Reference code: class Program {static void Main (string [] args) {ClassA ca = new ClassA (); ca. value = 88; ClassA ca 2 = new ClassA (); ca (ClassA. clone (); ca2.value = 99; Console. writeLine (ca. value + "-----" + ca2.value); // 88---99 ClassB cb = new ClassB (); cb. member. value = 13; ClassB cb2 = (ClassB) cb. clone (); cb2.Member. value = 7; Console. writeLine (cb. member. value. toString () + "------" + cb2.Member. va Lue. toString (); // shallow copy: 7---7 deep copy: 13----7 Console. read () ;}} public class ClassA: ICloneable {public int value = 0; public object Clone () {return this. memberwiseClone () ;}} public class ClassB: ICloneable {public ClassA Member = new ClassA (); public object Clone () {// shallow copy return this. memberwiseClone (); // deep copy ClassB obj = new ClassB (); obj. member = (ClassA) Member. clone (); return obj ;}analysis: On In this example, the ca object is copied by ca. The result is as shown in the Code: the change of the field of the Intermediate Value Type of ca does not affect the field of ca. In the class ClassB, reference the type Member. If you use the clone method in the ClassA, only the shortest copy is implemented. In the above reference code, we can see that: the change in the member of cb2 affects cb. However, when you use the reference code for deep copy, the change to the cb2 member will not affect cb. Find a comprehensive example on the Internet and compare it to explain the depth copy: instance 1: public class Sex: ICloneable {private string _ policx; public string ready X {set {_ folder x = value;} get {return _ folder x ;}// public object Clone () // {// return this. memberwiseClone (); //} public class Person: ICloneable {private Sex sex = new Sex (); public int aa = 3; public string between X {set {sex. required x = value;} get {return sex. optional X ;}} private string _ PName; p Ublic string PName {set {this. _ PName = value;} get {return this. _ PName ;}} public void ShowPersonInfo () {Console. writeLine ("-------------------------"); Console. writeLine ("Name: {0} Sex: {1}", _ PName, this. pSex); Console. writeLine ("-------------------------"); Console. writeLine (this. aa);} // copy public object Clone () {return this. memberwiseClone ();} // deeply copy public object DeepClone () {Person n EwP = new Person (); newP. PName = this. _ PName; newP. required x = this. extends X; return newP ;}} class Program {static void Main (string [] args) {Console. writeLine ("original object:"); Person p = new Person (); p. PName = "Lee"; p. longitude x = "male"; p. showPersonInfo (); // original object: lee male 3 // shallow copy Person copy = (Person) p. clone (); // deeply copy Person dcopy = (Person) p. deepClone (); Console. writeLine ("modified original object:"); p. PName = "Zhao"; p. pSex = "female"; p. Aa = 1; p. showPersonInfo (); // zhao female 1 Console. writeLine ("modified shortest object:"); copy. showPersonInfo (); // lee female 3 Console. writeLine ("modified deep copy object:"); dcopy. showPersonInfo (); // lee male 3 Console. writeLine ("directly copy object:"); Person PP = p; PP. showPersonInfo (); // zhao female 1 Console. readLine () ;}}analysis: first, it should be pointed out that adding the Clone method and not adding to the class Sex in the above example has no effect on the calculation result of the instance. In the class Person, the PName field of the reference type is a string type. The quote type contains the limit X field and the value type is aa. Initial Value: lee male 3 (advance) modification value: zhao female 1 light copy value: lee female 3 deep copy value: lee male 3 Direct Copy value: zhao female 1 result: the above can be said to summarize and compare the several types that are frequently encountered in the deep copy. I believe I can learn some knowledge after some experiences. Instance 2: class Program {static void Main (string [] args) {int [] numbers = {2, 3, 4, 5 }; int [] numbersCopy = new int [5]; numbers. copyTo (numbersCopy, 0); numbersCopy [2] = 0; int [] numbers1 = {2, 3, 4, 5}; int [] numbersClone1 = (int []) numbers1.Clone (); numbersClone1 [2] = 0; Console. write (numbers [2] + "---" + numbersCopy [2]); // 4---0 Console. write (numbers1 [2] + "---" + numbersClone1 [2]); // 4--0 // copying an array is also a reference transfer, point to the same address: int [] numbers2 = {2, 3, 4, 5}; int [] numbers2Copy = numbers2; numbers2Copy [2] = 0; Console. write (numbers2 [2]); // 0 Console. write (numbers2Copy [2]); // 0 Console. read ();}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.