Deep copy and shortest copy in C,

Source: Internet
Author: User

Deep copy and shortest copy in C,

1. Basic concepts:

First, we should know what Deep Copy and Shallow Copy are ).

A. Shallow Copy Shadow Clone: only copies the basic type of the object. The object type still belongs to the original reference.


B. Deep Copy (Deep Copy): Copies the basic class of the object, and also copies the object in the original object. A new object is generated completely.

The difference between deep copy and shallow copy is the processing of reference copy. Deep copy creates and assigns values to fields of the corresponding value type in the original object in the new object. A new reference type is not created for a shallow copy, and the same class type reference is returned. The deep copy operation creates a new object and returns the reference words of the new object.

2. Implementation of deep copy and light copy:

From the above concepts, we have learned the differences between C # Deep Copy and Shallow Copy. This determines that the two have different implementation methods.

For Value Type:


A. Shallow copy: The Value Type field in the object is copied to the new object by assigning values.
B. Deep copy: The Value Type field in the object is copied to the new object by assigning values. The same as the shortest copy.

For reference types:
 

A.Shortest copy: The MemberwiseClone method creates a shallow copy. The method is to create a new object. If the field is of the value type, the field is copied one by one. If the field is of reference type, copy and reference the original object, and the original object references the same object.

B.Deep copy: Copy the object application, or copy the actual content of the object, that is, create a new object that changes the content of the original object.
In this case, you need to implement the Clone method provided in the ICloneable interface.

The difference is that the mechanism is different when the implementation of the Reference type is deep copy and shallow copy. The former is the implementation of the MemberwiseClone method, and the latter is the Clone method provided in the ICloneable interface through inheritance, implements in-depth object copying.

 

3. Code Implementation

Shallow copy instance:

C # code Replication
Using System; namespace Prototype_Shallow {// because we already have this interface in FCL, we may not define the new Prototype. public class ConcretePrototype1: ICloneable {private int m_ID; public int ID {get {return this. m_ID ;}} public ConcretePrototype1 (int id) {this. m_ID = id;} public object Clone () {return this. memberwiseClone () ;}} public class ConcretePrototype2: ICloneable {private int m_ID; public int ID {get {return this. m_ID ;}} public ConcretePrototype2 (int id) {this. m_ID = id;} public object Clone () {return this. memberwiseClone ();}}}

Code Description:

Our prototype inherits the ICloneable interface and implements the only method Clone in the interface. In this way, we can create the object ConcretePrototype1 p1 = new ConcretePrototype1 (1 );

ConcretePrototype1 c1 = (ConcretePrototype1) p1.Clone ();

First, we created the object p1, And then we obtained the object c1 through the Cologne method of p1, which is a kind of shortest copy (because MemberwiseClone is shortest copy ).

Deep copy instance:

C # code Replication
namespace Prototype_Deep{       using System.Collections;       public class ConcretePrototype : ICloneable       {              private int m_ID;              public int ID              {                     get                     {                            return this.m_ID;                     }              }               private ArrayList m_arrayList = new ArrayList();               public ConcretePrototype(int id)              {                     this.m_ID = id;                     this.m_arrayList.Add("FirstObject");                     this.m_arrayList.Add("SecondObject");                     // ...              }               public object Clone()              {                     ConcretePrototype c = new ConcretePrototype(this.ID);                     c.m_arrayList = new ArrayList();                     c.m_arrayList.Add("FirstObject");                     c.m_arrayList.Add("SecondObject");                     return c;              }               public ConcretePrototype DeepClone(){                     return (ConcretePrototype)this.Clone();              }       }}

Code Description:

The code shows how to implement deep copy. The principle of deep copy is that you need new for the referenced fields (before new, think about whether it can be implemented in a created mode that you have learned before, this is a good habit), and then copy the objects in the field one by one, so loop copy is easy to appear, so deep copy is more difficult than shallow copy.

The client can

ConcretePrototype p = new ConcretePrototype (1 );

ConcretePrototype c = p. DeepClone ();

To clone a new object.

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.