. NET Deep replication and light Replication

Source: Internet
Author: User

. NET Deep replication and light Replication
Overview:
In. NET, object replication is provided on the premise that the ICloneable interface is implemented. The ICloneable interface has a Clone method,
You can override the custom COPY method in the class. There are two implementation methods for object replication: Deep replication and shallow replication.

Deep replication and light replication:

The source object shares an object with the Copied object. Changes to any of these objects will affect the other object. The pointer is copied.
Deep replication: the source object and the copy object are independent of each other. The new object allocates a memory space and copies the content of the source object. Any changes to an object will not affect another object.


Deep replication and data types:

The MemberwiseClone method creates a superficial copy of the current object by creating a new object and then copying the non-static fields of the current object to the new object.
If the field is of the value type, perform a step-by-step copy on the field. If the field is of reference type, the referenced object is copied but not referenced. Therefore, the original object and its counterparts reference the same object. There are two implementation methods for object replication: Light replication and light replication.

C # contains two types of variables: value type variables and reference type variables (classes, interfaces, arrays ). For the former, replication is a deep copy, while for the latter, the general replication is only a shallow copy, equivalent to passing only one reference pointer. Therefore, it is also the most troublesome for the latter to perform deep replication. Specifically, the Clone method provided in the ICloneable interface must be implemented for it.

Illustration:


Code Description: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3Ryb25nPkMj1tC1xMr91 + rotate/rotate + rotate = "brush: java;"> public class ShallowCopy: ICloneable {public int [] s = {1, 2, 4}; public Object Clone () {return this. memberwiseClone ();} public void Display () {foreach (int I in s) {Console. write (I + ",");} Console. writeLine ();}}Client: (1)


Client: (2)


Through obj1.s [1] = 1 and obj2.s [2] = 1, we can see that as long as any instantiated object modifies the referenced object, all are subject to the last modification.


Deep replication:

Public class DeepCopy: ICloneable {public int [] s = {1, 2, 3, 4}; public DeepCopy () {} private DeepCopy (int [] s) {this. s = (int []) s. clone ();} public Object Clone () {// construct a new DeepCopy Object return new DeepCopy (this. s);} public void Display () {foreach (int I in s) {Console. write (I + ",");} Console. writeLine ();}}}
Client:

If the object of deep replication instantiation is modified, for example, obj1.s [1] = 1, the object obj1 is modified and does not affect obj2.


Summary:The replication function allows you to create a specific object from one object without knowing the details of the creation. The prototype we learned is the embodiment of the replication function.

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.