.NET Framework Class Library
Object..::.MemberwiseClone Method

Creates a shallow copy of the current Object.

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

Visual Basic (Declaration)
Protected Function MemberwiseClone As Object
Visual Basic (Usage)
Dim returnValue As Object

returnValue = Me.MemberwiseClone()
C#
protected Object MemberwiseClone()
Visual C++
protected:
Object^ MemberwiseClone()
JScript
protected function MemberwiseClone() : Object

Return Value

Type: System..::.Object
A shallow copy of the current Object.
Remarks

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.

Examples

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();
   }
}
Visual C++
using namespace System;
ref class MyBaseClass
{
public:
   static String^ CompanyName;
   int age;
   String^ name;
};

ref class MyDerivedClass: public MyBaseClass
{
public:
   static void Test()
   {

      // Creates an instance of MyDerivedClass and assign values to its fields.
      MyDerivedClass^ m1 = gcnew MyDerivedClass;
      m1->age = 42;
      m1->name = "Sam";

      // Performs a shallow copy of m1 and assign it to m2.
      MyDerivedClass^ m2 = safe_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());
   }
}
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
See Also

Reference

Tags :


Community Content

Sylvain Rodrigue
MemberwiseClone and strings
string is a reference type but MemberwiseClone manages it like if it were a value type : it is the object (the characters themself) that is copied, not the reference to it.




Tags :

BenjaminOravetz
MemberwiseClone and strings
The System.String type is an immutable reference type. The MemberwiseClone method behaves the same way with System.String as any other reference type. MemberwiseClone does not treat System.String as a value-type.
Tags :

Pavel Lang
MemberwiseClone and strings
I believe that MemberwiseClone method behaves the same way with System.String as any other reference type. In this way observer cannot see any difference between character-to-character and immutable reference copy.
This code demonstrates use of MemberwiseClone():
	public class CloneTest : ICloneable
{
public string text;
public decimal number;
public int integer;

public override string ToString()
{
return String.Format("{0} : {1} : {2}", text, number, integer);
}

object ICloneable.Clone()
{
return this.MemberwiseClone();
}

public CloneTest Clone()
{
return (CloneTest)((ICloneable)this).Clone();
}
}

class Program
{
static void Main(string[] args)
{
CloneTest a = new CloneTest();
a.integer = 1;
a.number = 1.2m;
a.text = "This is CloneTest object";

CloneTest b = a.Clone();
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(object.ReferenceEquals(a, b));
Console.WriteLine(object.ReferenceEquals(a.text, b.text));
b.text = "Reference is now changed";
Console.WriteLine(b);
Console.WriteLine(object.ReferenceEquals(a.text, b.text));
Console.ReadLine();
}
}
This code writes:
This is CloneTest object : 1,2 : 1
This is CloneTest object : 1,2 : 1
False
True
Reference is now changed : 1,2 : 1
False

Page view tracker