1. Basic Concepts:
First we should look at what is called deep copy and shallow copy (shallow copy).
A. Shallow copy (shallow copy Shadow Clone): Copies only the base type of the object, the object type, and still belongs to the original reference.
B. Deep copy: The base class of the object is not tightly copied, and the objects in the original object are also copied. Completely produce new objects.
A deep copy differs from a shallow copy in that the reference copy is processed, and the deep copy is created in the new object and is assigned a field of the corresponding value type in the object. A shallow copy does not create a new reference type and returns the same type reference. A deep copy re-creates the new object and returns the reference word for the new object.
2. Deep copy and shallow copy implementation mechanism:
From the concept above, we understand the difference between a C # deep copy and a shallow copy (the deeper copy and shallow copy). This also determines that the two have different ways of achieving it.
For value types:
A. Shallow copy : The field of the value type in the object is copied to the new object by direct implementation, such as assignment of values.
b. Deep copy : The field of the value type in the object is copied to the new object by direct implementation, such as assignment. Same as shallow copy
For reference types:
A. shallow copy : The MemberwiseClone method creates a shallow copy by creating a new object that performs a bit-wise copy of the field if the field is of value type. If the field is a reference type, the copy references the original object, referencing the same object as the original object.
B. deep copy : Copy object application, also copy object actual content, that is, create a new change object does not affect the original object's content
This situation requires that the Clone method provided in the ICloneable interface be implemented for it.
The difference is in the implementation of the reference type of deep copy and shallow copy of the mechanism, the former is the MemberwiseClone method implementation, the latter is implemented through the inheritance of the ICloneable interface provided by the Clone method to achieve deep copy of the object.
3. Code implementation
Shallow Copy Example:
C # codeCopy
UsingSystem;NamespacePrototype_shallow{//Because we already have such an interface in the FCL, so we don't define a new prototype. Public ClassConcreteprototype1:icloneable{Private Intm_id;Public IntId{Get{Return This. m_id; }} PublicConcretePrototype1 (IntId{This. m_id=Id } Public ObjectClone (){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 specific prototypes have inherited the interface ICloneable, but also implemented the interface only one method Clone. We can create objects like this on the client ConcretePrototype1 p1 = new ConcretePrototype1 (1);
ConcretePrototype1 C1 = (ConcretePrototype1) p1. Clone ();
First we created the object P1, and then we got the object C1 by using the P1 Cologne method , which is a shallow copy (because memberwiseclone is a shallow copy).
Deep Copy Example:
C # code replication
NamespacePrototype_deep{UsingSystem.Collections;Public ClassConcreteprototype:icloneable{Private Intm_id;Public IntId{Get {Return This. m_id; }} PrivateArrayList m_arraylist= NewArrayList ();PublicConcreteprototype (IntId{This. m_id=IdThis. 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 a deep copy, the principle of deep copy is that for those fields you need to new(new to think about is not able to use the previous learning of a created pattern implementation, which is a good habit) one out, Then the object in this field one by one copies, so it is easy to have a circular copy, so deep copy is more difficult than a shallow copy.
Clients can use the
Concreteprototype p = new concreteprototype (1);
Concreteprototype C = P.deepclone ();
To implement cloning of a new object.
Deep copy and shallow copy in C #