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
- Resume Rbill = New Resume ("Bill");
- Rbill.setpersonalinfo ("man", "11");
- Rbill.setworkexperience ("2015-1-2-2015-12-10", "IBM");
- Resume Rcindy = (resume) rbill.clone ();
- Rcindy.setworkexperience ("2015-1-2 to 2015-11-11", "Microsoft");
- Resume Rll = (resume) rbill.clone ();
- Rll.setpersonalinfo ("Rll", "99");
- Rll.setworkexperience ("2015-1-2 to 2015-11-11", "Oracle");
- Rbill.display ();
- Rcindy.display ();
- Rll.display ();
- Console.read ();
The code for the class is as follows:
[CSharp]View Plaincopy
- Public class Resume:icloneable
- {
- private string name;
- private string age;
- private string sex;
- //private string workdate;
- //private string company;
- private workexperience work;
- Public Resume (string strName)
- {
- this.name = strName;
- Work = new Workexperience (); //Instantiate a Workexperience object while initializing the resume.
- }
- //provides a private constructor for the Clone method call to clone the data.
- Private Resume (workexperience work)
- {
- this.work = (workexperience) work. Clone ();
- }
- public void Setpersonalinfo (string sex, string age)
- {
- this.sex = sex;
- this.age = age;
- }
- public void Setworkexperience (string workdate, string company)
- {
- Work. Workdate = workdate;
- Work.company = Company;
- }
- public void Display ()
- {
- Console.WriteLine ("{0},{1},{2}", Name, sex, age);
- Console.WriteLine ("{0},{1}", work. Workdate, Work.company); //reference the work class's properties, fields.
- }
- Public Object Clone ()
- {
- 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
- Obj.name = name;
- Obj.sex = sex;
- //obj.workdate = workdate;
- Obj.age = age;
- return obj;
- //return (object) this. MemberwiseClone ();
- }
- }
- class Workexperience:icloneable
- {
- private string workdate;
- private String Company;
- public string workdate
- {
- get { return workdate;}
- set {workdate = value;}
- }
- public string Company
- {
- get { return company;}
- set {company = value;}
- }
- Public Object Clone ()
- {
- return (object) this. MemberwiseClone ();
- }
- }
See here, there is no enlightened bright it?
Ok,that ' s it! Thank for your attention!
Deep copy vs Shallow copy (MemberwiseClone method Introduction)