Deepclone and shadowclone (downmoon)

Source: Internet
Author: User
Tags mscorlib

Msdn:
Arraylist. Clone Method


Create arraylist
.

Namespace: system. Collections
Assembly: mscorlib (in mscorlib. dll)

Returns a superficial copy of The arraylist.

The superficial copies of a set only copy the elements of the Set (whether they are reference type or value type), but do not copy the referenced objects. The reference in the new set points to the same object as the reference in the original set.

In contrast, the deep copies of a set COPY Copies these elements and all the content directly or indirectly referenced by them.

The description is as follows:

The following is a complete example:

Using
System;

Using
System. Collections. Generic;

Using
System. text;

Using
System. IO;

Using
System. runtime. serialization. formatters. Binary;


Namespace
Cloneobjecttest

...
{



//
Just added to show the difference between shallow and deep cloning


[Serializable ()]


Class
Samplereftype


...
{


Public
 
Int
Val;

}




[Serializable ()]


Class
Employee


...
{


Public
 
String
M_name;


Public
Int16 m_age;


Public
Samplereftype someobject;



Public
 
Void
Print ()


...
{

Console. writeline (
"
Name:
"
 
+
M_name );

Console. writeline (
"
Age:
"
 
+
M_age );

Console. writeline (
"
Some value:
"
 
+
Someobject. Val );

}




//
This is one way to do deep cloning. But works only if


//
Objects and its references are serializable



Public
Employee deepclone ()


...
{

Memorystream m
=
 
New
Memorystream ();

Binaryformatter B
=
 
New
Binaryformatter ();

B. serialize (m,
This
);

M. Position
=
 
0
;


Return
(Employee) B. deserialize (m );

}




//
Do shallow cloning



Public
Employee shallowclone ()


...
{


Return
(Employee)
This
. Memberwiseclone ();

}




Static
 
Void
Main (
String
[] ARGs)


...
{

Samplereftype objref
=
 
New
Samplereftype ();

Objref. Val
=
 
1000
;

Employee obja
=
 
New
Employee ();

Obja. m_name
=
 
"
Manoj
"
;

Obja. m_age
=
 
23
;

Obja. someobject
=
Objref;

Employee objb
=
Obja. deepclone ();

Employee objc
=
Obja. shallowclone ();


//
Objb is a deep clone. So chages made to objref wocould not affect objb's value.


//
But objc is shallow cloned. So, if the objref is changed, so is the value in


//
Objc. So objc shocould print 2000 whereas objb shocould print 1000


Objref. Val
=
 
2000
;



//
Updating some state


Objc. m_age
=
 
100
;

Objb. m_name
=
 
"
New name
"
;


Console. writeline (
"
Original object:
"
);

Obja. Print ();

Console. writeline ();

Console. writeline (
"
Shallow cloned object:
"
);

Objc. Print ();

Console. writeline ();

Console. writeline (
"
Deep cloned object:
"
);

Objb. Print ();

}



}



}


 

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.