C # shallow copy and deep copy

Source: Internet
Author: User

From today on, I will share the content in this book with you every day, and add my own understanding, hoping to help more blind programmers like me. Today, let's talk about object copy in C #. The so-called object copy is actually creating a copy for the object. in C #, there are two types of copy, namely, shallow copy and deep copy; the so-called shallow copy is to copy all the fields in the object to the new copy object. There is a difference between the value type and the reference type for the shallow copy. After the value of the Value Type field is copied to the copy, modification in the copy does not affect the value corresponding to the source object. However, if a field of the reference type is copied to the copy, it is a reference of the reference type, rather than a reference object, after the field value of the reference type is modified in the copy, the value of the source object is also modified. Deep copy also copies all fields in the object to the copy object. However, no matter the value type field of the object or the reference type field, it is re-created and copied, it does not affect the source object itself. Of course, no matter which type of copy is used, Microsoft recommends that you use the method of inheriting the ICloneable interface to explicitly tell the caller whether the object can be copied. Of course, the ICloneable interface only provides a method declared as Clone. We can implement the shortest copy or deep copy in the Clone method as needed. Below is a case of shortest copy, copy code 1 class Student: ICloneable 2 {3 public string IDCode {get; set;} 4 5 public int Age {get; set;} 6 7 public Grent {get; set;} 8 9 # region copy subject 10 public object Clone () 11 {12 return this. memberwiseClone (); 13 // throw new NotImplementedException (); 14} 15 # endregion16 17} 18 19 class Grent20 {21 public String Name {get; set;} 22 23 public override string ToString () 24 {25 return this. name; 26} 27} copy Code call copy code Student stu1 = new Student () {IDCode = "lily", Age = 24, Grent = new Grent () {Name = "three shifts in five years" }}; Student stu2 = stu1.Clone () as Student; if (stu2 = null) {Console. writeLine ("Conversion failed"); Console. readKey (); return;} Console. writeLine (stu2.IDCode); Console. writeLine (stu2.Age); Console. writeLine (s Tu2.Grent. toString (); Console. writeLine ("assign a value to stu1 again"); stu1.IDCode = "Anagle"; stu1.Age + = 10; stu1.Grent. name = ""; Console. writeLine (stu2.IDCode); Console. writeLine (stu2.Age); Console. writeLine (stu2.Grent. toString (); Console. readKey (); the output result of the copied code is: lily 24, Class 3, Class 3, assigned the value of lily 24, class 2, class 24, and class 2 to stu1. Note that the IDCode attribute in Student is of the string type, in theory, the string type is the reference type, but because of the special nature of the reference type, Object. the MemberwiseClone method still creates a copy for him. In the process, we should regard the string as the value type. Because Grent in Student is the reference type, while the Name of Grent in stu1 is changed, the Name of the Grent in the copy stu2 is also changed. Student's deep copy has multiple clock implementation methods. The simplest method is to manually assign values to the values of the fields, but this method is prone to errors, that is, if fields of the type change or increase or decrease, the copy method also changes accordingly. Therefore, we recommend that you use serialization for deep copy. The implementation code is as follows: copy the Code [Serializable] class Student: ICloneable {public string IDCode {get; set;} public int Age {get; set;} public Grent {get; set ;} # region copy subject /// <summary> /// deep copy /// </summary> /// <returns> </returns> public Student DeepClone () {using (Stream objectStream = new MemoryStream () {IFormatter formatter = new BinaryFormatter (); formatter. serialize (objectStream, this); objectStream. seek (0, SeekOrigin. begin); return formatter. deserialize (objectStream) as Student;} public object Clone () {return this. memberwiseClone () ;}# endregion} copy Code call DeepClone method copy code Student stu1 = new Student () {IDCode = "lily", Age = 24, Grent = new Grent () {Name = "three shifts in five years" }}; Student stu2 = stu1.DeepClone () as Student; if (stu2 = null) {Console. writeLine ("Conversion failed"); Console. readKey (); return;} Console. writeLine (stu2.IDCode); Console. writeLine (stu2.Age); Console. writeLine (stu2.Grent. toString (); Console. writeLine ("assign a value to stu1 again"); stu1.IDCode = "Anagle"; stu1.Age + = 10; stu1.Grent. name = ""; Console. writeLine (stu2.IDCode); Console. writeLine (stu2.Age); Console. writeLine (stu2.Grent. toString (); Console. readKey (); copy the code. The output result is: lily, Class 3, Class 3, class 1, class 4, Class 3, Class 4

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.