Asp. NET deep copy and shallow copy analysis _ Practical Tips

Source: Internet
Author: User
Tags shallow copy

In this paper, deep asp.net and shallow copy are analyzed. Share to everyone for your reference. The specific analysis is as follows:

I have not been clear about the difference between depth replication and shallow copy exactly where, today thoroughly understand this thing, write to and home for mutual encouragement.

If you do not understand the difference between value types and reference types, please see http://www.jb51.net/article/57471.htm, originally wanted to write their own, but just saw this article written very comprehensive, do not write their own.

As we all know, object is a common base class for all classes, and it has a method of MemberwiseClone (), which uses

We can use this method to achieve the effect of the light copy.

Here's an example to illustrate the difference between a shallow copy and a deep copy:

Copy Code code 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, a content class, a Val with a value type int, and a class is a Cloner class that has a member of the content type, and then there's a constructor that initializes the member, and finally there's a getcopy method, Copy yourself by MemberwiseClone method.

Let's call the Cloner class with a piece of code:

Copy Code code as follows:
static void Main (string[] args)
{
Cloner Source = new Cloner (10);
Cloner target = (cloner) source. Getcopy ()//Returns an object type that requires a 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:

We can see that we copy class source to target through the getcopy () function, but when we change source, the value of the source output changes as well. From this we can conclude that we only copy the reference through MemberwiseClone (), that is, the mycontent of source and target is the same object instance. This is the shallow copy, so how do we achieve deep replication, in the. NET framework, provides us with the ICloneable interface.

First, let's look at the ICloneable interface:

Copy Code code as follows:
Summary:
Supports cloning, which creates a new instance of a class with the same value as an existing instance.
[ComVisible (True)]
public interface ICloneable
{
Summary:
Creates a new object that is a copy of the current instance.
//
return Result:
A new object that is a copy of this instance.
Object Clone ();
}

In the example above, we just need to modify some of the code:

Copy Code code 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 on the above, the annotation code is a shallow copy, the following for the depth of replication, we see that the difference is to use the mycontent.val of this instance to regenerate the instance returned to the target, the test results are:

Here if the members of the mycontent are not a value type, then we need to do the depth as follows:

Copy Code code as follows:
public Object Clone ()
{
Cloner cloned = new Cloner ();
Cloned. Mycontent = Mycontent.clone ();
return cloned;
}

In the case of deep copy and shallow copy, the key is to create a new object instance to go back, instead of returning the original object instance.

I hope this article will be described to you. NET program design helps.

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.