Examples of deep and shallow copies in C #

Source: Internet
Author: User
Tags copy reference shallow copy

There are two types of variables in C #, one is a value type variable, the other is a reference type variable. For the former, copy is part of the overall replication;
For the latter, general copy is only light copy, which is equivalent to passing only one reference pointer. So for the latter to really copy the time, but also the most troublesome, specifically, you must implement the ICloneable interface provided in the Clone method, the sample code is as follows:

public class Test
{
public class Person:icloneable
{
public int ID;
public int age;
public void Show ()
{
Console.WriteLine ("Id:{0} Age: {1}", ID, aged);
}
public Object Clone ()
{
Person Newperson = new person ();
Newperson.id = this.id;
Newperson.age = This.age;
return Newperson;
}
}
public static void Showpersons (person[] persons)
{
for (int i=0; i<persons. GetLength (0); i++)
{
Persons[i]. Show ();
}
}
static void Main (string[] argv)
{
Random rnd = new Random (unchecked ((int) DateTime.Now.Ticks));
person[] persons = new PERSON[4];
for (int i=0;i<persons. GetLength (0); i++)
{
Persons[i] = new Person ();
Persons[i].id = rnd. Next ()%10;
Persons[i].age = rnd. Next ()%50;
}
Print the original array
Console.WriteLine ("Print original array");
Showpersons (persons);
Deep copy
person[] personscopied = new Person[4];
for (int i=0;i<personscopied.getlength (0); i++)
{
Personscopied[i] = (person) persons[i]. Clone ();
}
Persons. CopyTo (personscopied, 0);
Console.WriteLine ("An array of deep copies");
Showpersons (personscopied);
Shallow copy
Person[] personscloned = (person[]) persons. Clone ();
Console.WriteLine ("Array of shallow copies");
Showpersons (personscloned);
Make modifications to the original array
Persons[2].id + 10;
Console.WriteLine ("Print modified original array");
Showpersons (persons);
Console.WriteLine ("Deep copy of the array should not change");
Showpersons (personscopied);
Console.WriteLine ("The array of shallow copies should be changed");
Showpersons (personscloned);
}
}

The results of a run:

For the above example, when a deep copy is made, a new space must first be created for the object array, while the object members in the array are allocated new space by the corresponding method that implements the interface, and the new object array from deep copy will not change because of the original array, instead, For personscloned that have only shallow copy in the comment row, they are affected by the original array.

Also pay attention to persons. CopyTo (personscopied, 0); This is not a deep copy of the personscopied, which is a copy of a reference to the same object, because in C # for an array of reference types, the reference type of the array itself and the reference type of the element in the group cannot be confused, and the array is just a container. So the container itself and what is stored in the container are instantiated separately. Oh, I have a lot of days of trouble posted up to do a memo ha



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.