Creates a shallow copy of the current Object.
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
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 is CloneTest object : 1,2 : 1This is CloneTest object : 1,2 : 1FalseTrueReference is now changed : 1,2 : 1False