Deep copy and shortest copy on the. net platform

Source: Internet
Author: User

Basic concepts:

Shallow copy: The field of the object is copied, and the object referenced by the field is not copied. The Copied object and the original object are only referenced by different names, but they share an object. Changes to any object will affect another object. Most of the reference types are implemented in the shortest copy mode. The value assignment between reference type objects is to copy a copy of the Object Reference address, and the object instance is still the same.

Deep copy: the object's child segment is copied, and the object referenced by the field is also copied. Deep copy creates the structure of the entire source object. The copy object and the original object are independent of each other and do not share any instance data. Modifying an object does not affect the other object. The value assignment operation between value types performs deep copy.

Reference code for basic concepts:
Copy codeThe Code is as follows:
Class Program
{
Static void Main (string [] args)
{
Student s1 = new Student ("li", 23 );

// Shallow copy
Student s2 = s1;
S2.Age = 27;
S1.ShowInfo (); // li's age is 27

// Deep copy
Int I = 12;
Int j = I;
J = 22;
Console. WriteLine (I); // 12

Console. Read ();
}
}

Class Student
{
Public string Name;
Public int Age;

Public Student (string name, int age)
{
Name = name;
Age = age;
}

Public void ShowInfo ()
{
Console. WriteLine ("{0}'s age is {1}", Name, Age );
}
}

Analysis:

In the above example, instance s2 has made a shortest copy of s1 and changed the Age field in instance s2, which affects the Age field in instance s1.

In deep copy, it is only a simple value assignment between value types. Changes made to "j" do not change the value of "I.

Implementation of deep copy:

Copy codeThe Code is as follows:
Public object Clone ()
{
Return this. MemberwiseClone ();
}

MemberwiseClone: Creates a superficial copy. The process is to create a new object, and then copy the non-static fields of the current object to the new object. If the field is of the value type, perform a one-on-one copy of the field. If the field is of the reference type, copy the reference but do not copy the reference object.

Reference code:
Copy codeThe Code is as follows:
Class Program
{
Static void Main (string [] args)
{
ClassA ca = new ClassA ();
Ca. value = 88;
ClassA Ca 2 = new ClassA ();
Ca. Clone ();
Ca2.value = 99;
Console. WriteLine (ca. value + "-----" + ca2.value); // 88---99

ClassB cb = new ClassB ();
Cb. Member. value = 13;

ClassB cb2 = (ClassB) cb. Clone ();
Cb2.Member. value = 7;
Console. WriteLine (cb. Member. value. ToString () + "------" + cb2.Member. value. ToString (); // shortest copy: 7---7 deep copy: 13----7

Console. Read ();
}
}

Public class ClassA: ICloneable
{
Public int value = 0;

Public object Clone ()
{
Return this. MemberwiseClone ();
}
}

Public class ClassB: ICloneable
{
Public ClassA Member = new ClassA ();

Public object Clone ()
{
// Shallow copy
Return this. MemberwiseClone ();

// Deep copy
ClassB obj = new ClassB ();
Obj. Member = (ClassA) Member. Clone ();
Return obj;
}
}

Analysis:

In the preceding example, the ca object is copied by ca 2, which implements deep copy. The result is as shown in the Code: the change of the field of the Intermediate Value Type of ca does not affect the field of ca.

In the class ClassB, reference the type Member. If you use the clone method in the ClassA, only the shortest copy is implemented. In the above reference code, we can see that: the change in the member of cb2 affects cb. However, when you use the reference code for deep copy, the change to the cb2 member will not affect cb.

Find a comprehensive example on the Internet and compare it to explain the depth copy:

Instance 1:

Copy codeThe Code is as follows:
Public class Sex: ICloneable
{
Private string _ bytes X;
Public string comment x
{
Set {_ struct x = value ;}
Get {return _ response X ;}
}

// Public object Clone ()
//{
// Return this. MemberwiseClone ();
//}
}

Public class Person: ICloneable
{

Private Sex sex = new Sex ();
Public int aa = 3;

Public string comment x
{
Set {sex. sex x = value ;}
Get {return sex. writable 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;
}
}

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

P. ShowPersonInfo (); // original object: lee male 3

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

Console. WriteLine ("modified original object :");
P. PName = "Zhao ";
P. Category X = "female ";
P. aa = 1;
P. ShowPersonInfo (); // zhao female 1

Console. WriteLine ("modified shortest object :");
Copy. ShowPersonInfo (); // lee female 3

Console. WriteLine ("modified deep copy object :");
Dcopy. ShowPersonInfo (); // lee male 3

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

Console. ReadLine ();
}
}

Analysis:

First, it should be pointed out that adding the Clone method or not in the Sex class in the previous example does not affect the operation result in the instance.

In the class Person, the PName field of the reference type is a string type. The quote type contains the limit X field and the value type is aa.

Initial Value: lee male 3 (copy in depth first)

Value modified: zhao female 1

Light copy value: lee female 3

Deep copy value: lee male 3

Direct Copy value: Zhao female 1

Result: The above can be said to summarize and compare the several types that are frequently encountered in the deep copy. I believe I can learn some knowledge after some understanding.

Instance 2:
Copy codeThe Code is as follows:
Class Program
{
Static void Main (string [] args)
{
Int [] numbers = {2, 3, 4, 5 };
Int [] numbersCopy = new int [5];
Numbers. CopyTo (numbersCopy, 0 );
NumbersCopy [2] = 0;

Int [] numbers1 = {2, 3, 4, 5 };
Int [] numbersClone1 = (int []) numbers1.Clone ();
NumbersClone1 [2] = 0;

Console. Write (numbers [2] + "---" + numbersCopy [2]); // 4---0
Console. Write (numbers1 [2] + "---" + numbersClone1 [2]); // 4--0


// Copying an array, that is, transferring a reference, points to the same address.
Int [] numbers2 = {2, 3, 4, 5 };
Int [] numbers2Copy = numbers2;
Numbers2Copy [2] = 0;

Console. Write (numbers2 [2]); // 0
Console. Write (numbers2Copy [2]); // 0

Console. Read ();
}
}

Do not perform analysis for the moment.

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.