Deep copy vs Shallow copy (MemberwiseClone method Introduction)

Source: Internet
Author: User

The MemberwiseClone method, which belongs to the namespace system, exists in assembly mscorlib.dll. The return value is System.Object. This means: Create a shallow copy of the current object object.

The official explanation in MSDN is:

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.

For example, consider an object called X that references objects A and B. object B refers to object C in turn. a shallow copy of X creates a new object, X2, that also references objects A and B. In contrast, a deep copy of X creates a new object, X2, that references new objects A2 and B2 (replicas of A and B, respectively). B2 also refers to the new object C2,C2 is a copy of C. This example illustrates the difference between shallow and deep copy operations.

There are many ways to implement a deep copy operation, provided that the shallow copy operation is performed by the MemberwiseClone method but does not meet your needs. These requirements include:

1. Call the class constructor of the object you want to copy to create a second object containing the value of the property raised from the first object. This assumes that the value of the object is entirely defined by the class constructor.

2. Call the MemberwiseClone method to create a shallow copy of the object, and then specify the new object whose value is the same, and the value of any property or field of the original object is a reference type. The Deepcopy method in the example illustrates this approach.

3. Serialize the object to be deeply copied, and then restore the serialized data to another object variable.

4. Deep copy operations performed using reflection with recursion.

I'm guessing you've been dizzy since you've seen this. Oh, this rookie is such a drop ... In their unremitting efforts, and from the example of the experiment to understand, the original so-called shallow copy and deep copy is so simple Ah!

In fact, it's easy to understand the relationship between the shortcuts and the source files we use in the Windows operating system! I believe that everyone from playing the computer may have encountered such an embarrassing situation, is to use the U disk copy of the computer files, and then happily go to print, and then a print shop Open the U disk copy to the file, "Nani!" I can't open it! It is the original copy of a shortcut, the amount of & ... "(haha ...) Speaking of which, it is estimated that some people are very sympathetic! Right? )

Shortcut: In fact, the equivalent is the reference source file, the shortcut does not exist in the source file object, just the address of a source file, this address point to the source file, when you double-click, Windows will go to your computer based on this address to find the source file and open. Just copy a shortcut, which is equivalent to a shallow copy.

Copy the source file: Copy the file's data, which is called deep replication.

Still don't understand? Let's see an example! (Inspired by: The prototype model of the big talk design model--resume copy)

Look at the class diagram first:

Shallow copy:

The client code is as follows

[CSharp]View Plaincopy
  1. Resume Rbill = New Resume ("Bill");
  2. Rbill.setpersonalinfo ("man", "11");
  3. Rbill.setworkexperience ("2015-1-2-2015-12-10", "IBM");
  4. Resume Rcindy = (resume) rbill.clone ();
  5. Rcindy.setworkexperience ("2015-1-2 to 2015-11-11", "Microsoft");
  6. Resume Rll = (resume) rbill.clone ();
  7. Rll.setpersonalinfo ("Rll", "99");
  8. Rll.setworkexperience ("2015-1-2 to 2015-11-11", "Oracle");
  9. Rbill.display ();
  10. Rcindy.display ();
  11. Rll.display ();
  12. Console.read ();

The code for the class is as follows:

[CSharp]View Plaincopy
  1. Public class Resume:icloneable
  2. {
  3. private string name;
  4. private string age;
  5. private string sex;
  6. //private string workdate;
  7. //private string company;
  8. private workexperience work;
  9. Public Resume (string strName)
  10. {
  11. this.name = strName;
  12. Work = new Workexperience ();  //Instantiate a Workexperience object while initializing the resume.
  13. }
  14. //provides a private constructor for the Clone method call to clone the data.
  15. Private Resume (workexperience work)
  16. {
  17. this.work = (workexperience) work.  Clone ();
  18. }
  19. public void Setpersonalinfo (string sex, string age)
  20. {
  21. this.sex = sex;
  22. this.age = age;
  23. }
  24. public void Setworkexperience (string workdate, string company)
  25. {
  26. Work. Workdate = workdate;
  27. Work.company = Company;
  28. }
  29. public void Display ()
  30. {
  31. Console.WriteLine ("{0},{1},{2}", Name, sex, age);
  32. Console.WriteLine ("{0},{1}", work.   Workdate, Work.company);  //reference the work class's properties, fields.
  33. }
  34. Public Object Clone ()
  35. {
  36. Resume obj = new Resume (this.work); //Call the private constructor, complete the clone, and assign a value to the relevant field of the Resume object, returning a deep copy of the Resume object
  37. Obj.name = name;
  38. Obj.sex = sex;
  39. //obj.workdate = workdate;
  40. Obj.age = age;
  41. return obj;
  42. //return (object) this.  MemberwiseClone ();
  43. }
  44. }
  45. class Workexperience:icloneable
  46. {
  47. private string workdate;
  48. private String Company;
  49. public string workdate
  50. {
  51. get { return workdate;}
  52. set {workdate = value;}
  53. }
  54. public string Company
  55. {
  56. get { return company;}
  57. set {company = value;}
  58. }
  59. Public Object Clone ()
  60. {
  61. return (object) this.  MemberwiseClone ();
  62. }
  63. }

See here, there is no enlightened bright it?

Ok,that ' s it! Thank for your attention!

Deep copy vs Shallow copy (MemberwiseClone method Introduction)

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.