C # Deep copy through serialization

Source: Internet
Author: User
I do not have a clear understanding of the shortest copy and deep copy operations. To put it simply, I think it is like this:

Shallow copy: When a referenced member is copied, only the address of the referenced member from the source object is copied to the new object, therefore, changing the referenced member in the new object will affect the source object (except for assigning values to referenced members ).

Deep copy: When the referenced member creates a new referenced object to the new object, and copies the value of the referenced object from the source object to the referenced object of the new object, therefore, modifying the referenced member in the new object does not affect the source object.

The concept is also simple. I think what you are struggling with is how to implement deep copy? The implementation of the shortest copy is very simple. It would be great to call object. memberwiseclone. I found an example on the Internet to implement deep copy through serialization and modified it myself. You are welcome to discuss this.

This sectionProgramThe output is:

 

Obja1.refclass. Field = 10
Obja2.refclass. Field = 10
Obja1.refclass. Field = 10
Obja2.refclass. Field = 30
Objb1.refclass. Field = 20
Objb2.refclass. Field = 10

 

Note that the serializable attribute must be added to the classb class that implements deep copy and Its referenced member refclass class in this example.

 

UsingSystem;
UsingSystem. collections;
UsingSystem. Collections. Generic;
UsingSystem. text;
UsingSystem. IO;
UsingSystem. runtime. serialization. formatters. Binary;

Namespace Leleapplication1
{
// The serializable attribute must be added to the referenced class; otherwise, serialization cannot be implemented.
[Serializable]
Class Refclass
{
Private Int Field;
Public Int Field
{
Get { Return Field ;}
Set { This . Field = Value ;}
}
}

// shortest copy example
class classa: icloneable
{< br> private refclass;
Public refclass
{< br> Get { return refclass ;}
set {refclass = value ;}
}

PublicClassa ()
{
Refclass= NewRefclass ();
}

Public ObjectClone ()
{
//Call memberwiseclone to implement shallow copy
ReturnMemberwiseclone ();
}
}

// for deep copy examples, the serializable attribute must be added, otherwise, serialization cannot be implemented.
[serializable]
class classb: icloneable
{< br> private refclass;
Public refclass
{< br> Get { return refclass ;}< BR >}

PublicClassb ()
{
Refclass= NewRefclass ();
}

// Deep copy
Public Object Clone ()
{
// Create a memory stream
Memorystream MS = New Memorystream ();
// Serialization in binary format
Binaryformatter BF = New Binaryformatter ();
BF. serialize (MS, This );
// Deserializing the current instance to an object
Ms. Seek ( 0 , 0 );
Object OBJ = BF. deserialize (MS );
// Disable memory stream
Ms. Close ();
Return OBJ;
}
}

Class Program
{
Static Void Main ( String [] ARGs)
{
// Shortest copy
Classa obja1 = New Classa ();
Obja1.refclass. Field = 20 ;
Classa obja2 = (Classa) obja1.clone ();
// Modify the refclass field value in the new object, and change the value in the source object.
Obja2.refclass. Field = 10 ;
Console. writeline ( " Obja1.refclass. Field = " + Obja1.refclass. Field. tostring ());
Console. writeline ( " Obja2.refclass. Field = " + Obja2.refclass. Field. tostring ());
// Reassigning values to the refclass of the new object does not affect the source object.
Obja2.refclass = New Refclass ();
Obja2.refclass. Field = 30 ;
Console. writeline ( " Obja1.refclass. Field = " + Obja1.refclass. Field. tostring ());
Console. writeline ( " Obja2.refclass. Field = " + Obja2.refclass. Field. tostring ());
// Deep copy
Classb objb1 = New Classb ();
Objb1.refclass. Field = 20 ;
Classb objb2 = (Classb) objb1.clone ();
// Modify the refclass field value in the new object. The value in the source object remains unchanged.
Objb2.refclass. Field = 10 ;
Console. writeline ( " Objb1.refclass. Field = " + Objb1.refclass. Field. tostring ());
Console. writeline ( " Objb2.refclass. Field = " + Objb2.refclass. Field. tostring ());
Console. readkey ();
}
}
}

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.