(C #) prototype mode-deep copy and shallow copy

Source: Internet
Author: User

1. Prototype mode

Use the prototype instance to specify an instance of the created object, and create a new object by copying the prototypes.

* Prototype mode hides the details of creating objects and improves performance.

* Shallow copy: All variables of the copied object contain the same value as the original object, and all references to other objects point to the original object.

* Deep copy: The reference to the copied object points to the new object, not the original referenced object.

*. NET provides the only method under the Iconeable interface clone can easily complete the prototype mode implementation.

2. Example:

namespaceprototype Mode _ shallow copy {classProgram {Static voidMain (string[] args) {Resume a=NewResume ("Big Bird"); A.setpersonalinfo ("male"," at"); A.setworkexperience (" -","XX Company"); Resume b=(Resume) A.clone (); B.setworkexperience ("male","yy company");            A.display (); B.display ();//a shallow copy makes the referenced copy point to the same space, and A's work experience is covered by BConsole.read (); }    }        classWorkexperience {Private stringworkdate;  Public stringWorkdata {Get{returnworkdate;} Set{workdate =value;} }        Private stringCompany ;  Public stringCompany {Get{returnCompany ;} Set{company =value;} }    }    classresume:icloneable {Private stringname; Private stringsex; Private stringAge ; Privateworkexperience work;  PublicResume (stringname) {             This. Name =name; work=Newworkexperience (); }         Public voidSetpersonalinfo (stringSexstringAge ) {             This. Sex =sex;  This. Age =Age ; }         Public voidSetworkexperience (stringWorkdata,stringCompany ) {work. Workdata=Workdata; Work.company=Company ; }         Public voidDisplay () {Console.WriteLine ("{0} {1} {2}", name, sex, age); Console.WriteLine ("Shallow copy: {0} {1}", work.        Workdata, Work.company); }         Public ObjectClone () {return(Object) This.        MemberwiseClone (); }    }}

At this point the output, a, and all details are identical.

namespacePrototype Mode _ deep copy {classProgram {Static voidMain (string[] args) {Resume a=NewResume ("Big Bird"); A.setpersonalinfo ("male"," at"); A.setworkexperience (" -","XX Company"); Resume b=(Resume) A.clone (); B.setworkexperience ("male","yy company");            A.display (); B.display ();//a shallow copy makes the referenced copy point to the same space, and A's work experience is covered by BConsole.read (); }    }    classworkexperience:icloneable {Private stringworkdate;  Public stringWorkdata {Get{returnworkdate;} Set{workdate =value;} }        Private stringCompany ;  Public stringCompany {Get{returnCompany ;} Set{company =value;} }         PublicObject Clone () {return(Object) This.        MemberwiseClone (); }    }    classresume:icloneable {Private stringname; Private stringsex; Private stringAge ; Privateworkexperience work;  PublicResume (stringname) {             This. Name =name; work=Newworkexperience (); }         PublicResume (workexperience work) { This. Work =(workexperience) work.        Clone (); }         Public voidSetpersonalinfo (stringSexstringAge ) {             This. Sex =sex;  This. Age =Age ; }         Public voidSetworkexperience (stringWorkdata,stringCompany ) {work. Workdata=Workdata; Work.company=Company ; }         Public voidDisplay () {Console.WriteLine ("{0} {1} {2}", name, sex, age); Console.WriteLine ("Deepreplication: {0} {1}", work.        Workdata, Work.company); }         Public ObjectClone () {Resume obj=NewResume ( This. Work); Obj.name= This. Name; Obj.sex= This. Sex; Obj.age= This. Age; returnobj; }    }}

b in modifying the attributes in work experience is not affected in a.

(C #) prototype mode-deep copy and shallow copy

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.