C # differences between deep copy and light copy and Examples

Source: Internet
Author: User

Deep copy means that the source object and the copy object are independent of each other. Any changes to one object will not affect the other object. For example, a person named Zhang San was cloned with him (assuming the law permits it) and called Li Si. Neither Zhang San's arm nor legs nor Li Si's arm nor legs would affect another person. Typical objects are Value objects, such as Int32, Double, struct, and Enum.

A shallow copy refers to the sharing of an object between the source object and the copy object. It means that the referenced variables are different (different names ). Any changes to one of the objects will affect another object. For example, a person was originally named Zhang San and later renamed Li Si, but he was still the same person. Whether Zhang San lacks his arm and legs or Li Si lacks his arm and legs, this person is unlucky.

C # contains two types of variables: value type variables and reference type variables. For the former, copy is a full copy, while for the latter, the general copy is only a small copy, equivalent to passing only one reference pointer. Therefore, it is also the most time-consuming for the latter to perform true copy. Specifically, the Clone method provided in the ICloneable interface must be implemented for the latter.

Let's look at the definition:
Light copy (Shadow Clone): Only copies the basic type of the object. The object type still belongs to the original reference.
Deep copy (deep clone): Copies the basic class of the object without any constraints, and also copies the objects in the original object, that is, they are completely generated by the new object.

Differences between shallow copy and deep copy
Copying a field of the numeric type in an object to a new object means copying a reference field of the object to the target object. If you change the value of the referenced field in the target object, it will be reflected in the original object, that is, the corresponding field in the original object will also change. The difference between deep copy and shallow copy is the processing of reference. Deep copy creates a new field in the new object that is the same as the corresponding field in the original object (the content is the same, that is to say, this reference is different from the original object reference. When we change this field in the new object, it does not affect the content of the corresponding field in the original object. Therefore, there are two different processing methods for the prototype: shallow copy and deep copy of objects.

The following example shows the difference between the shallow copy and the deep copy. The Code is as follows:

Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Text;

Namespace WindowsApplication1
{
Class Program
{
Public class Sex
{
Private string _ bytes X;
Public string comment x
{
Set
{
_ Bytes X = value;
}
Get
{
Return _ rows X;
}
}

}

Public class Person: ICloneable
{

Private Sex _ Sex x = new Sex ();
Public int aa = 1213;

Public string comment x
{
Set
{
_ Required X. Required x = value;
}
Get
{
Return _ rows X. Rows X;
}
}
Private string _ PName;
Public string PName
{
Set
{
This. _ PName = value;
}
Get
{
Return this. _ PName;
}
}

Public void ShowPersonInfo ()
{
Console. WriteLine ("-------------------------");
Console. WriteLine ("Name: {0} Sex: {1}", _ PName, this. writable X );
Console. WriteLine ("-------------------------");
Console. WriteLine (this. aa );
}
// Shallow copy
Public object Clone ()
{
Return this. MemberwiseClone ();
}
// Deep copy
Public object DeepClone ()
{
Person newP = new Person ();
NewP. PName = this. _ PName;
NewP. Rows x = this. Rows X;
Return newP;
}

}

Static void Main (string [] args)
{
Console. WriteLine ("original object :");
Person p = new Person ();
P. PName = "JackLee ";
P. Male x = "male ";

P. ShowPersonInfo ();

// Shallow copy
Person copy = (Person) p. Clone ();
// Deep copy
Person dcopy = (Person) p. DeepClone ();

Console. WriteLine ("modified original object :");
P. PName = "JackZhao ";
P. Category X = "female ";
P. aa = 1111;
P. ShowPersonInfo ();

Console. WriteLine ("modified shortest object :");
Copy. ShowPersonInfo ();
Console. WriteLine ("modified deep copy object :");
Dcopy. ShowPersonInfo ();

Console. WriteLine ("directly copy object :");
Person PP = p;
PP. ShowPersonInfo ();

Console. ReadLine ();

}

}
}

Well, let's talk about deep copy and shallow copy. The project is not finished yet. Hurry up first. The code can be directly copied to the Project for running.

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.