C # Deep copy

Source: Internet
Author: User
1. Deep copy and light copyCopy is usually referred to as copy or clone. Copying an object is a new object that is "identical" from an existing object. Although all objects are replication objects, the new objects copied by different replication methods are not exactly the same, and there are some differences within the objects. There are two common copy methods: Deep copy and shallow copy. What is the difference between them? In msdn, the clone method of the iclone interface is described as follows: In Deep replicas, all objects are duplicated, while in superficial replicas, only top-level objects are duplicated, and the objects below the top level include references. It can be seen that The difference is whether sub-objects are copied.How can this be understood? Next I will verify the difference between the two through code with sub-objects. First, define two types: Student and classroom. The student type includes classroom, and enable these two types to implement the custom deep copy interface (ideepcopy) respectively) and ishallowcopy ). The class diagram is as follows:

The definition code is as follows:

/// <Summary> /// deep copy interface /// </Summary> interface ideepcopy {object deepcopy ();}
/// <Summary> /// shallowcopy interface /// </Summary> interface ishallowcopy {object shallowcopy ();}
/// <Summary> // Classroom Information // </Summary> class classroom: ideepcopy, ishallowcopy {publicint roomid = 1; publicstring roomname = "room1 ";
Publicoverridestring tostring () {return "roomid =" + roomid + "\ troomname =" + roomname;} publicobject deepcopy () {classroom r = new classroom (); R. roomid = This. roomid; R. roomname = This. roomname; return r;} publicobject shallowcopy () {// directly use the built-in shallowcopy method to return returnthis. memberwiseclone ();}}
Class student: ideepcopy, ishallowcopy {// to simplify, use the public field publicstring name; publicint age; // custom type, assume that each student has only one classroompublic classroom room = new classroom ();
Public student () {} public student (string name, int age) {This. name = Name; this. age = age;} publicobject deepcopy () {student s = new student (); S. name = This. name; S. age = This. age; S. room = (classroom) This. room. deepcopy (); Return s;} publicobject shallowcopy () {returnthis. memberwiseclone ();}
Publicoverridestring tostring () {return "name:" + name + "\ Tage:" + age + "\ t" + room. tostring ();}
} Copy the code

 

Test code:

 

Test code

Student S1 = new student ("Vivi", 28); console. writeline ("S1 = [" + S1 + "]"); Student S2 = (student) s1.shallowcopy (); // student S2 = (student) s1.deepcopy (); console. writeline ("S2 = [" + S2 + "]"); // The content of S2 and S1 is the same as that of console. writeline ("---------------------------"); // modify s2.name = "tianyue"; s2.age = 25; s2.room. roomid = 2; s2.room. roomname = "room2"; console. writeline ("S1 = [" + S1 + "]"); console. writeline ("S2 = [" + S2 + "]"); // print two objects again to compare the console. readline (); copy the code

 

 

Running result:. shallowcopys1 = [name: Vivi Age: 28 roomid = 1 roomname = room1] S2 = [name: Vivi Age: 28 roomid = 1 roomname = room1] Rule S1 = [Name: vivi Age: 28 roomid = 2 roomname = room2] S2 = [name: tianyue age: 25 roomid = 2 roomname = room2] B. deepcopys1 = [name: Vivi Age: 28 roomid = 1 roomname = room1] S2 = [name: Vivi Age: 28 roomid = 1 roomname = room1] --------------------------- S1 = [Name: Vivi Age: 28 roomid = 1 roomname = room1] S2 = [name: tianyue age: 25 roomid = 2 roomname = room2] as shown in the preceding results, during Deep copy, the two objects are completely "separated". Changing one of them will not affect the other object. During the shallow copy, the two objects are not completely "separated" and the content of the top-level object is changed, it does not affect another object, but changes the content of the sub-object at the same time. The difference is determined by the replication memory or the replication pointer when the sub-object is copied. Deep copy re-allocates a memory space for the sub-object and copies the content. The light copy only points the pointer to the original sub-object. As follows: 2. Copying and assigning valuesIn most object-oriented language assignment operations, a reference is passed, that is, the pointer address of the object is changed, and the memory is not copied and no replication operation is performed. We can see that the difference between the shallow copy operation and the value assignment operation is Whether the top-level object is copied or not.Of course, there are some exceptions, such as assignment operator in the type definition, or some type conventions passing by value, just like struct and enumeration types in C. The assignment operation is as follows:  

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.