Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Object.MemberwiseClone Method

Creates a shallow copy of the current Object.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

'Declaration
Protected Function MemberwiseClone As Object
'Usage
Dim returnValue As Object

returnValue = Me.MemberwiseClone
protected Object MemberwiseClone ()
protected function MemberwiseClone () : Object
Not applicable.

Return Value

A shallow copy of the current Object.

The MemberwiseClone method creates a shallow copy by creating a new object, and then copying the nonstatic fields of the current object to the new object. If a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the referred object is not; therefore, the original object and its clone refer to the same object.

For example, consider an object called X that references objects A and B. Object B, in turn, references object C. A shallow copy of X creates new object X2 that also references objects A and B. In contrast, a deep copy of X creates a new object X2 that references the new objects A2 and B2, which are copies of A and B. B2, in turn, references the new object C2, which is a copy C. Use a class that implements the ICloneable interface to perform a deep or shallow copy of an object.

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


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

import System.*;

class MyBaseClass implements ICloneable
{
    public static String companyName = "My Company";
    public int age;
    public String name;
} //MyBaseClass

class MyDerivedClass extends MyBaseClass
{
    public static void main(String[] args) throws CloneNotSupportedException
    {
        // 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();
    } //main
} //MyDerivedClass

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

Show:
© 2014 Microsoft