Creates a shallow copy of the current Object.
Namespace:
System
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Protected Function MemberwiseClone As Object
Dim returnValue As Object
returnValue = Me.MemberwiseClone()
protected Object MemberwiseClone()
protected:
Object^ MemberwiseClone()
protected function MemberwiseClone() : 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
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();
}
}
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();
}
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 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.
.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
Reference