C # Reading Notes-simple copy and deep copy

Source: Internet
Author: User

Prerequisites:
Copying an object from a variable to another variable by value, rather than copying objects by reference (that is, copying objects in the same way as the structure) can be very complicated. Because an object may contain references of many objects, such as fields
This will involve a lot of annoying processing operations. Copying each member from one object to another may not succeed, because some members may be of the reference type.

Light replication: simply copying objects by members can be derived from system. the memberwiseclone () method of the object is implemented. This is a protected method, but it is easy to define a public method to call this method on the object. The replication function of this method becomes light replication.

The advantage of light replication: you do not need to reference the object type.

Shortest method:

using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2{    public class Content    {        public int Val;    }    public class Cloner    {        public Content MyContent = new Content();        public Cloner(int newVal)        {            MyContent.Val = newVal;        }        public object GetCopy()        {            return MemberwiseClone();        }    }    public class Program    {        static void Main(string[] args)        {            Cloner mySource = new Cloner(5);            Cloner myTarget = (Cloner)mySource.GetCopy();            Console.WriteLine("MyTarget.MyContent.val = {0}",myTarget.MyContent.Val);            mySource.MyContent.Val = 2;            Console.WriteLine("MyTarget.MyContent.val = {0}", myTarget.MyContent.Val);            Console.ReadKey();        }    }}
Here mysource. mycontent. Val = 2 changes the value of the public field Val, so the generated result is as follows:
MyTarget.MyContent.val = 5
MyTarget.MyContent.val = 2
The above uses the replication reference type to demonstrate how to use the replication.
If you want to make the generated value 5, you need to use deep replication. about deep replication: to use deep replication, you must implement the icloneable interface of the class. This interface has a method clone (). This method returns an object type without parameters, the signature is the same as the getcopy () method used above.
1 using system;
2 using system. Collections. Generic;
3 using system. text;
4
5 namespace leleapplication2
6 {
7. Public class content
8 {
9 Public int val;
10
11}
12
13 public class cloner: icloneable
14 {
15 public content mycontent = new content ();
16
17 Public cloner (INT newval)
18 {
19 mycontent. Val = newval;
20}
21 public object getcopy ()
22 {
23 return memberwiseclone ();
24}
25
26 # region icloneable Member
27
28 public object clone ()
29 {
30 cloner clonedcloner = new cloner (mycontent. Val );
31 return clonedcloner;
32}
33
34 # endregion
35}
36
37 public class Program
38 {
39 static void main (string [] ARGs)
40 {
41 cloner mysource = new cloner (5 );
42 cloner mytarget = (cloner) mysource. Clone ();
43 console. writeline ("mytarget. mycontent. Val = {0}", mytarget. mycontent. Val );
44 mysource. mycontent. Val = 2;
45 console. writeline ("mytarget. mycontent. Val = {0}", mytarget. mycontent. Val );
46 console. readkey ();
47}
48}
49}

This is because the cloner implements icloneable and uses deep replication to create a new object that acts as a copy of the current instance (clonedcloner here). Finally, during the test of deep replication, the clone () method is called, but the getcopy () method is not called. Therefore, mytarget is called. mycontent. the Val value is always new cloner (5), rather than the last defined value 2. after learning this knowledge point, I think that the icloneable () interface exists to call the clone () method. If you need to copy a value of a reference type or value type to an object, let it directly implement the method of this interface. This may not be understood correctly. I hope you can communicate with me. Description of the clone () method of icloneable () interface: clone can be implemented either as a deep copy or as a superficial copy. In a deep copy, all objects are repeated. In a superficial copy, only top-level objects are repeated, and objects below the top-level include references. The clone result must be of the same type as the original Instance or be compatible with the original Instance.

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.