Shallow and deep copies in C #

Source: Internet
Author: User

#中有两种类型变量, one is a value type variable and one is a reference type variable.

For the former, copy is a copy of the whole, whereas for the latter, the general copy is only a shallow copy, only the reference address, which is equivalent to passing only one reference pointer. Therefore, it is also the most troublesome to make a real copy of the latter, specifically, it is necessary to implement the Clone method provided in the ICloneable interface to produce a completely new object.

Shallow copy (Shadow clone): Copies only the base type of the object, the object type, and still belongs to the original reference.
Deep copy (Deep clone): The base class of the object is not tightly copied, and the objects in the original object are copied as well. That is, it is entirely a new object.

The difference between a shallow copy and a deep copy:

A shallow copy is a copy of a field of a numeric type in an object to a new object, whereas a reference field in an object refers to a reference to a target object that replicates it. If you change the value of a reference field in the target object, he will be reflected in the original object, meaning that the corresponding field in the original object will also change.

A deep copy differs from a shallow copy in that of a reference, a deep copy creates a new object with the same field as the corresponding field in the original object (with the same content), meaning that the reference is different from the original object. When we change this field in the new object, it doesn't affect the contents of the corresponding field in the original object.

So there are two different ways of dealing with prototype patterns (prototype pattern): shallow and deep copies of objects

The MemberwiseClone method creates a shallow copy by creating a new object and then copying the non-static field of the current object to the new object. If the field is of value type, a bitwise copy of the field is performed. If the field is a reference type, the referenced object is copied and not copied, so the original object and its replica refer to the same object. Deep copy, which implements the ICloneable interface. ICloneable can be used for deep and shallow copies.

. NET provides a icloneable interface with a clone () method that you can implement to implement your own cloning methods, such as deep clones or shallow clones, MemberwiseClone () is a method in the object class to implement a shallow clone of a class.

Using System.Collections.Generic;    #region//Deep clone MemberwiseClone () Clone ()//<summary>//Abstract Class Animal//</summary>        Public abstract class Animal {public int i;        public double D;        public byte B;        Public string[] s;    Public abstract Animal Clone (); }///<summary>//Subclass dog///</summary> public class Dog:animal {public Dog (i            NT I, double D, byte B, string S1, string s2) {this.i = i;            THIS.D = D;            this.b = b;            String[] ms = {s1, s2};        THIS.S = ms; public override Animal Clone () {return (Animal) this.        MemberwiseClone (); }} #endregion protected void Page_Load (object sender, EventArgs e) {#region//Deep clone shallow clone memberwise            Clone () Clone () Animal a1 = new Dog (1, 2, 3, "A", "B"); Response.Write ("Animal A1 ' s members:" +a1.i+ "" +a1.d+"" + a1.b+ a1.s[0]+ a1.s[1]+ "<br/>");            Animal A2; a2 = A1.            Clone ();            Response.Write ("Animal A2 ' s members:" + a2.i+ a2.d+ a2.b+ a2.s[0]+ a2.s[1]+ "<br/>");            Response.Write ("Do a1.i = 9;a1.s[0] = C" + "<br/>");             a1.i = 9;            A1.s[0] = "C";            Response.Write ("Animal A1 ' s members:" +a1.i+a1.d+ a1.b+ a1.s[0]+ a1.s[1]+ "<br/>");            Response.Write ("Animal A2 ' s members:" + a2.i+ a2.d+ a2.b+ a2.s[0]+ a2.s[1]+ "<br/>");            Response.Write ("Do a2.i = 8;a2.s[1] =d" + "<br/>");             a2.i = 8;            A2.S[1] = "D";            Response.Write ("Animal A1 ' s members:" + a1.i + a1.d + a1.b + a1.s[0] + a1.s[1] + "<br/>");            Response.Write ("Animal A2 ' s members:" + a2.i + a2.d + a2.b + a2.s[0] + a2.s[1] + "<br/>"); #endregion}

  

Output

Animal A1 ' s members:1 2 3AB
Animal A2 ' s MEMBERS:123AB
Do a1.i = 9;a1.s[0] = C
Animal A1 ' s MEMBERS:923CB
Animal A2 ' s MEMBERS:123CB
Do a2.i = 8;a2.s[1] =d
Animal A1 ' s MEMBERS:923CD
Animal A2 ' s MEMBERS:823CD

If a deep clone is used, a simple class may have only a value type, but it is not possible to have a reference type at any point. Deep clones the more real way is to serialize and deserialize, and of course to use reflection, or an assignment to one. It is usually a method called deepcopy (). Take a look at the following example: Add a new method to the Dog Class Deepclone (), and the dog class to implement [Serialize]

Public Dog Deepclone ()
{
BinaryFormatter Bformatter = new BinaryFormatter ();
MemoryStream stream = new MemoryStream ();
Bformatter.serialize (stream, this);
Stream. Seek (0, Seekorigin.begin);
Return (DOG) bformatter.deserialize (stream);
}

Added in Page_Load:

#region//Clone () method in deep clone ICloneable interface
Dog dog1 = new Dog (1, 2, 3, "A", "B");
Response.Write ("Dog Dog1 ' s members:" + dog1.i + "" + dog1.d + "" + dog1.b + dog1.s[0] + dog1.s[1] + "<br/>");

Dog dog2 = Dog1. Deepclone ();
Response.Write ("Dog dog2 ' s members:" + dog2.i + dog2.d + dog2.b + dog2.s[0] + dog2.s[1] + "<br/>");

Response.Write ("Change dog1.i = 9;dog1.s[0] = C" + "<br/>");
dog1.i = 9;
Dog1.s[0] = "C";
Response.Write ("Dog Dog1 ' s members:" + dog1.i + "" + dog1.d + "" + dog1.b + dog1.s[0] + dog1.s[1] + "<br/>");
Response.Write ("Dog dog2 ' s members:" + dog2.i + dog2.d + dog2.b + dog2.s[0] + dog2.s[1] + "<br/>");

Response.Write ("Change dog2.i = 8;dog2.s[1] = D" + "<br/>");
dog2.i = 8;
DOG2.S[1] = "D";
Response.Write ("Dog Dog1 ' s members:" + dog1.i + "" + dog1.d + "" + dog1.b + dog1.s[0] + dog1.s[1] + "<br/>");
Response.Write ("Dog dog2 ' s members:" + dog2.i + dog2.d + dog2.b + dog2.s[0] + dog2.s[1] + "<br/>");

#endregion

Output

Dog Dog1 ' s members:1 2 3AB
Dog dog2 ' s MEMBERS:123AB
Change dog1.i = 9;dog1.s[0] = C
Dog Dog1 ' s Members:9 2 3CB
Dog dog2 ' s MEMBERS:123AB
Change dog2.i = 8;dog2.s[1] = D
Dog Dog1 ' s Members:9 2 3CB
Dog dog2 ' s MEMBERS:823AD

Shallow and deep copies in C #

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.