ASP. NET deep replication and shortest copy analysis, asp.net deep Replication

Source: Internet
Author: User

ASP. NET deep replication and shortest copy analysis, asp.net deep Replication

This article provides an in-depth analysis of ASP. NET deep replication and simple replication. Share it with you for your reference. The specific analysis is as follows:

I have never figured out the difference between deep replication and simple replication. Today I fully understand this and write it out to encourage me.

If you do not know the difference between the value type and the reference type, refer to the values first.

As we all know, an Object is a common base class of all classes. Its method is MemberwiseClone () and its purpose is

We can use this method to achieve the effect of simple replication.

Here is an example to illustrate the differences between the replication and replication:
Copy codeThe Code is as follows: 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 has only one Val of the Value Type int, And the other class is a Cloner class with a member of the Content type, then there is a constructor that can initialize members. Finally, there is a GetCopy method, which is used to copy itself through the MemberwiseClone method.

The following code calls the Cloner class:
Copy codeThe Code is as follows: 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 is:

We can see that we use the GetCopy () function to copy the source class to the target, but 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 codeThe Code is as follows: // summary:
// Supports cloning, that is, creating a new instance with the same value as an existing instance.
[ComVisible (true)]
Public interface ICloneable
{
// Summary:
// Create a new object as a copy of the current instance.
//
// Return result:
// As a new object for this instance copy.
Object Clone ();
}

In the above example, we only need to modify some code:
Copy codeThe Code is as follows: 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;
}
}

In order to make a difference, I put the previous code above. The annotated code is a simple copy, followed by a deep copy. We can see that the difference is that the MyContent of this instance is used. val re-generates the instance and returns it to the target. The test result is:

Here, if the MyContent member is not a value type, we still need to perform the depth, as shown below:
Copy codeThe Code is as follows: public object Clone ()
{
Cloner cloned = new Cloner ();
Cloned. MyContent = MyContent. Clone ();
Return cloned;
}
In this way, the key is to create a new object instance and return it instead of returning the original object instance.

I hope this article will help you with the. NET program design.

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.