Export (0) Print
Expand All

Object.MemberwiseClone Method

Creates a shallow copy of the current Object.

[Visual Basic]
Protected Function MemberwiseClone() As Object
[C#]
protected object MemberwiseClone();
[C++]
protected: Object* MemberwiseClone();
[JScript]
protected function MemberwiseClone() : Object;

Return Value

A shallow copy of the current Object.

Remarks

MemberwiseClone cannot be overridden, and is only accessible through this class or a derived class. Use a class that implements the ICloneable interface if a deep or shallow copy of an object needs to be publicly exposed to users.

A shallow copy creates a new instance of the same type as the original object, and then copies the nonstatic 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

The following code example shows how to copy an instance of a class using MemberwiseClone.

[Visual Basic] 

Imports System

Class MyBaseClass
   Public Shared CompanyName As String = "My Company"
   Public age As Integer
   Public name As String
End Class 'MyBaseClass

Class MyDerivedClass
   Inherits MyBaseClass

   Shared Sub Main()

      ' Creates an instance of MyDerivedClass and assign values to its fields.
      Dim m1 As New MyDerivedClass()
      m1.age = 42
      m1.name = "Sam"

      ' Performs a shallow copy of m1 and assign it to m2.
      Dim m2 As MyDerivedClass = CType(m1.MemberwiseClone(), MyDerivedClass)

   End Sub 'Main

End Class 'MyDerivedClass

[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();
   }
}

[C++] 
#using <mscorlib.dll>

using namespace System;

__gc class MyBaseClass
{
public:
   static String* CompanyName;
   int age;
   String* name;
};

__gc class MyDerivedClass : public MyBaseClass
{
public:
   static void Test()
   {
      // Creates an instance of MyDerivedClass and assign values to its fields.
      MyDerivedClass* m1 = new MyDerivedClass();
      m1->age = 42;
      m1->name = S"Sam";

      // Performs a shallow copy of m1 and assign it to m2.
      MyDerivedClass* m2 = __try_cast<MyDerivedClass*>(m1->MemberwiseClone());
   }
};

int main()
{
   MyDerivedClass::Test();
}

[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());
   }
}

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Object Class | Object Members | System Namespace | ICloneable

Show:
© 2014 Microsoft