C # shallow replication and deep Replication

Source: Internet
Author: User

Recently, I encountered a complicated copying problem of classes.

In. net, all classes have a memberwiseclone method that inherits the object. The memberwiseclone method provides shallow replication of classes. Why is it a light copy? When the memberwiseclone method is used for copying, a new variable is created for the value type in the class, and the referenced variable points to the original reference and does not create a new reference variable, for more information, see the following link.

Http://msdn.microsoft.com/zh-cn/library/system.object.memberwiseclone.aspx

Msdn provides a simple method to implement deep replication of classes that contain reference types. However, if a class contains the list attribute, list itself is a reference type, the "=" symbol does not generate a copy of the list, but only copies the reference. Therefore, an iteration is required to copy the entire list. Use the code to summarize deep replication:

Class Program
{
Static void main (string [] ARGs)
{
School school1 = New School ();
// Call the clone method of school to create a deep copy
School school2 = (school) school1.clone ();

// Modify the attributes of school1 to see if it will affect school2.
School1.president. Name = "Liu ";
School1.grades [0]. ID = 3;
School1.buildings [0] = "Third Floor ";

Console. Readline ();
}
}

Public class school: icloneable
{
Public String city {Get; set ;}
Public string name {Get; set ;}
Public President {Get; set ;}
Public list <Grade> grades {Get; set ;}
Public list <string> buildings {Get; set ;}

/// <Summary>
/// Initialize School
/// </Summary>
Public School ()
{
City = "City ";
Name = "City a No. 1 ";

President = new president
{
Age = 50,
Name = "Zhang"
};

Grades = new list <Grade> ();
Grades. Add (new grade {id = 1, name = "First Grade "});
Grades. Add (new grade {id = 2, name = ""});

Buildings = new list <string> ();
Buildings. Add ("1st Floor ");
Buildings. Add ("Floor 2 ");
}

/// <Summary>
/// Deep copy School
/// </Summary>
/// <Returns> deep copy </returns>
Public object clone ()
{
// Copy a copy.
School newschool = (school) This. memberwiseclone ();

// Call the clone method of President to create a new object for President
Newschool. President = (President) This. President. Clone ();

// Clear two lists
Newschool. Grades = new list <Grade> ();
Newschool. Buildings = new list <string> ();

// Add grades to the new school
Foreach (VAR grade in grades)
{
Newschool. Grades. Add (grade) grade. Clone (); // copy by calling the clone method of Grade
}

// Add buildings to the new school
Foreach (VAR building in buildings)
{
Newschool. Buildings. Add (building );
// Although the string type is a reference type. in the. NET Framework, the "=" symbol is used to create a new String object in the heap, so you can copy the string without using the clone method.
// Newschool. Buildings. Add (string) Building. Clone ());
}

Return newschool;
}
}

Public class grade: icloneable
{
Public int ID {Get; set ;}
Public string name {Get; set ;}

Public object clone ()
{
Return this. memberwiseclone ();
}
}

Public class president: icloneable
{
Public int age {Get; set ;}
Public string name {Get; set ;}

Public object clone ()
{
Return this. memberwiseclone ();
}
}

Public class person: icomparable
{
Public String firstname;
Public String lastname;

Public Person ()
{}

Public Person (string first, string last)
{
Firstname = first;
Lastname = last;
}

Public int compareto (Object OBJ)
{
Person Other = OBJ as person;
Int result = This. lastname. compareto (other. lastname );
This. memberwiseclone ();
Return result;
}
}

 

The above is the general usage method of deep replication. Using this method, we can find that there are more simple methods on the Internet, so we don't need to implement the clone method of the icloneable interface. serialization solves this problem. You can refer to this document in workshop.

Http://www.cnblogs.com/zhangji/archive/2011/02/23/1961897.html

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.