The difference analysis and example of C # deep copy and shallow copy

Source: Internet
Author: User
Deep copy means that the source and copy objects are independent of each other, and any changes to the object do not affect the other object. For example, a person named Zhang San, later with his cloning (assuming the law allows) another person, called John Doe, whether it is Zhang three short arms or legs or Li four lack of arms and legs will not affect another person. A typical value object, such as a predefined type int32,double, and a struct (struct), enumeration (enum), and so on.

A shallow copy is a source object that is shared with a copy object, and only refers to a variable (with a different name). Changes to any of these objects affect the other object. For example, a person at the beginning of the call Zhang San, later renamed John Doe, but still the same person, whether it is Zhang three missing arms or legs or Li four short legs, is this person unlucky.

There are two types of variables in C #, 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, which is equivalent to passing only one reference pointer. Therefore, the latter for the real copy of the time, but also the most troublesome, specifically, it must be implemented in the ICloneable interface to provide the Clone method.

Look at the definition:
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 the case of a reference, and a deep copy creates a new field in the new object that is the same as the corresponding field in the object (with the same content), meaning that the reference is different from the original object, and we do not affect the original pair when changing this field in the new object. The contents of the corresponding field in the image. So there are two different ways of dealing with prototype patterns: shallow and deep copies of objects.

The following example can clearly see the difference between a shallow copy and a deep copy, the code is as follows:

Using System; Using System.Collections.Generic; Using System.Text; Namespace WindowsApplication1 {Class Program {public class Sex {private String _pse             X                 public string Psex {set {_psex = value;                 } get {return _psex;             }}} public class Person:icloneable {private Sex _psex = new Sex ();             public int aa = 1213;                 public string Psex {set {_psex.psex = value;                 } get {return _psex.psex;             }} private string _pname;                 public string PName {set {this._pname = value;   }              get {return this._pname; }} public void Showpersoninfo () {Console.WriteLine ("----------------                 ---------");                 Console.WriteLine ("Name:{0} Sex:{1}", _pname, This.psex);                 Console.WriteLine ("-------------------------");             Console.WriteLine (THIS.AA); }//Shallow copy public Object Clone () {return this.             MemberwiseClone ();                 }//Deep copy public Object Deepclone () {Person newp = new person ();                 Newp.pname = This._pname;                 Newp.psex = This.psex;             return NEWP;             }} static void Main (string[] args) {Console.WriteLine ("Original object:");             Person p = new person ();             P.pname = "Jacklee";             P.psex = "male"; P.shoWpersoninfo ();             Shallow copy person copy = (person) p.clone ();              Deep copy person dcopy = (person) p.deepclone ();             Console.WriteLine ("Modified original object:");             P.pname = "Jackzhao";             P.psex = "female";             P.AA = 1111;              P.showpersoninfo ();             Console.WriteLine ("Modified Shallow Copy object:"); Copy.             Showpersoninfo ();             Console.WriteLine ("Modified Deep Copy object:"); Dcopy.             Showpersoninfo ();             Console.WriteLine ("Direct Copy object:");             Person PP = p; Pp.             Showpersoninfo ();          Console.ReadLine (); }     } }

OK, about the deep copy and the shallow copy here, the project is not finished, first grasp. The code can be copied directly to the project to run.

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.