Msdn:
Arraylist. Clone Method
CreateArraylist.
Namespace: system. Collections
ProgramSet: 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 IntVal;
}
[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 ();
}
}
}