| |||||
Object.MemberwiseClone MethodCreates a shallow copy of the current Object. [ VB ] <Serializable> <ClassInterface ( ClassInterfaceType.AutoDual ) > Protected Function MemberwiseClone ( ) As Object [ C# ] [ Serializable ] [ ClassInterface ( ClassInterfaceType.AutoDual ) ] Protected object MemberwiseClone ( ); [ C++ ] [ Serializable ] [ ClassInterface ( ClassInterfaceType.AutoDual ) ] protected: Object* MemberwiseClone ( ); [ JScript ] protected Serializable ClassInterface ( ClassInterfaceType.AutoDual ) function MemberwiseClone ( ) : Object; Return ValueA shallow copy of the current Object. RemarksThis method cannot be overridden; a derived class should implement the ICloneable interface if a shallow copy is not appropriate. MemberwiseClone is protected and, therefore, is accessible only through this class or a derived class. A shallow copy creates a new instance of the same type as the original object, and then copies the non-static fields of the original object. If the field is a value type, a bit-by-bit copy of the field is performed. If the field is a reference type, the reference is copied but the referred object is not; therefore, the reference in the original object and the reference in the clone point to the same object. In contrast, a deep copy of an object duplicates everything directly or indirectly referenced by the fields in the object. For example, if X is an Object with references to the objects A and B, and the object A also has a reference to an object M, a shallow copy of X is an object Y, which also has references to objects A and B. In contrast, a deep copy of X is an object Y with direct references to objects C and D, and an indirect reference to object N, where C is a copy of A, D is a copy of B, and N is a copy of M. The Type of the clone is the same as the type of the original Object. Example[ C#, JScript ] The following code example shows how to copy an instance of a class using MemberwiseClone. [ C# ] using System; class MyBaseClass { public static string CompanyName = "My Company"; public int age; public string name; } class MyDerivedClass: MyBaseClass { static void Main ( ) { // creates an instance of MyDerivedClass and assign values to its fields. MyDerivedClass m1 = new MyDerivedClass ( ); m1.age = 42; m1.name = "Sam"; // performs a shallow copy of m1 and assign it to m2. MyDerivedClass m2 = ( MyDerivedClass ) m1.MemberwiseClone ( ); } } [ JScript ] import System class MyBaseClass { public static var CompanyName : String = "My Company"; public var age : int; public var name : String; } class MyDerivedClass extends MyBaseClass { static function Main ( ) { // creates an instance of MyDerivedClass and assign values to its fields. var m1 : MyDerivedClass = new MyDerivedClass ( ); m1.age = 42; m1.name = "Sam"; // performs a shallow copy of m1 and assign it to m2. var m2 : MyDerivedClass = MyDerivedClass ( m1.MemberwiseClone ( ) ); } } See Also |
| ||||
Check out related books at Amazon
© 2000-2008 Rey Nuñez All rights reserved.
If you have any question, comment or suggestion
about this site, please send us a note
You can help support aspxtreme