Correct implementation of shallow copy and deep copy

Source: Internet
Author: User

Differences between a copy and a copy: A copy Value Type field does not affect the field corresponding to the source object. modifying a copy's reference type field affects the source object, because when the source object is copied to the copy object, it is a reference address of the reference type, that is, the two reference the same object. Deep copy: whether it is a field of the value type or reference type, modifying the copy object does not affect the source object. Even if it is a reference type, a new object reference is created. To create a custom type with the Clone copy capability, you must inherit the ICloneable interface, and then implement the Clone method as needed to implement the shortest copy or deep copy. Example of a shallow copy: copy the code namespace WebApplication {public class Employee: ICloneable {public string IDCode {get; set;} public int Age {get; set;} public Department {get; set;} // implement the ICloneable interface member public object Clone () {return this. memberwiseClone () ;}} public class Department {public string Name {get; set;} public override string ToString () {return this. name ;}} public partial class Web Form1: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {// initialize the Employee object employeeA Employee employeeA = new Employee () {IDCode = "A", Age = 10, department = new Department () {Name = "DepartmentA"}; // copy the Employee ID from employeeA. clone () as Employee; // modify the attributes of the employeeB object. IDCode = "B"; employeeA. age = 15; employeeA. department. name = "DepartmentB"; // output to verify Response. write (employeeB. IDCode); // A Response. write (employeeB. age); // 10 Response. write (employeeB. department. toString (); // DepartmentB }}copy the code from the output result to verify the result: 1. The IDCode is of the string reference type, Object. memberwiseClone still creates a copy for it. In the shortest copy, we can regard string as a value type. 2. The Department attribute of Employee is of the reference type. Changing the value in the source object employeeA will affect the deep copy of the copy object employeeB. We recommend that you use serialization for the deep copy example: copy the code // implement the ICloneable interface member public object Clone () {// shallow copy // return this. memberwiseClone (); // use serialization for deep copy of using (Stream objectStream = new MemoryStream () {IFormatter formatter = new BinaryFormatter (); formatter. serialize (objectStream, this); objectStream. seek (0, SeekOrigin. begin); return formatter. deserialize (objectS Tream) as Employee;} copy the code here I run the program according to the code in the book. The result is yellow pages. The prompt message is: the type "WebApplication. Employee" in is not marked as serializable. Because I have related development experience before, I know that it is because the entity class is not marked as a serialization attribute. Didn't I check this error when I compile the example? Or another reason? We can mark the object class to run successfully. This is to modify the value in the source object employeeA and will not affect the copy object employeeB. [Serializable] public class Employee: ICloneable [Serializable] public class Department

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.