. NET-based deep replication and shortest Replication

Source: Internet
Author: User

. NET-based deep replication and shortest Replication
Here is an example to illustrate the difference between simple replication and deep replication: copy the code 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 () ;}} here we have two classes, one Content class, and only one Val for the value type int, another class is a Cloner class, which has a member of the Content type, then a constructor can initialize the member, and finally a GetCopy method, which is used to copy itself through the MemberwiseClone method. The following code calls the Cloner class: copy the code static void Main (string [] args) {Cloner source = new Cloner (10); Cloner target = (Cloner) source. getCopy (); // The returned Object type requires type conversion. console. writeLine ("target. myContent. val = {0} ", target. myContent. val); source. myContent. val = 15; Console. writeLine ("target. myContent. val = {0} ", target. myContent. val); Console. readKey ();} the result of copying the code is: Copy. We can see that we use the GetCopy () function to Copy the source class to target, However, when we change the source, the value of the source output also changes. From this we can conclude that MemberwiseClone () copies only references, that is, the MyContent of source and target is the same object instance. This is simple replication. How can we implement deep replication? In the. NET Framework, we are provided with the ICloneable interface. First, let's take a look at the ICloneable interface: Copy code // Abstract: // supports cloning, that is, using the same value as the existing instance to create a new instance of the class. [ComVisible (true)] public interface ICloneable {// Abstract: // create a new object as a copy of the current instance. //// Return result: // as a new object of this instance copy. Object Clone () ;}copy the code in the above example, we only need to modify some code: copy the code public class Cloner: ICloneable {public Content MyContent = new Content (); public Cloner (int newVal) {MyContent. val = newVal;} // public object GetCopy () // {// return MemberwiseClone (); //} public object Clone () {Cloner cloned = new Cloner (MyContent. val); return cloned ;}} copy the Code. In order to make a difference, I put the previous code on it. The annotated code is a simple copy, followed by a deep copy, the difference is that the MyContent of this instance is used. val The test result is: QQ20141116144216. Here, if the MyContent member is not a value type, we still need to perform the depth, as shown below: public object Clone () {Cloner cloned = new Cloner (); cloned. myContent = MyContent. clone (); return cloned;} This is the case for deep replication and shortest replication. The key is to create a new object instance and return it instead of returning the original object instance. Do you understand?

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.