Mono Class Library: System.Object Overview | Members

System.Object.MemberwiseClone Method

Creates a shallow copy of the current instance.

protected object MemberwiseClone ()

Returns

A shallow copy of the current instance. The run-time type (the exact type) of the returned object is the same as the run-time type of the object that was copied.

Remarks

object.MemberwiseClone creates a new instance of the same type as the current instance and then copies each of the object's non-static fields in a manner that depends on whether the field is a value type or a reference type. If the field is a value type, a bit-by-bit copy of all the field's bits is performed. If the field is a reference type, only the reference is copied. The algorithm for performing a shallow copy is as follows (in pseudo-code):

for each instance field f in this instance
if (f is a value type)
bitwise copy the field
if (f is a reference type)
copy the reference
end for loop

Note: This mechanism is referred to as a shallow copy because it copies rather than clones the non-static fields.

Because object.MemberwiseClone implements the above algorithm, for any object, a, the following statements are required to be true:

  • a.MemberwiseClone() is not identical to a.
  • a.MemberwiseClone().GetType() is identical to a.GetType().

object.MemberwiseClone does not call any of the type's constructors.

Note: If object.Equals(object) has been overridden, a.MemberwiseClone().Equals(a) might return false .

Usage

For an alternate copying mechanism, see ICloneable .

object.MemberwiseClone is protected (rather than public) to ensure that from verifiable code it is only possible to clone objects of the same class as the one performing the operation (or one of its subclasses). Although cloning an object does not directly open security holes, it does allow an object to be created without running any of its constructors. Since these constructors might establish important invariants, objects created by cloning might not have these invariants established, and this can lead to incorrect program behavior. For example, a constructor might add the new object to a linked list of all objects of this class, and cloning the object would not add the new object to that list -- thus operations that relied on the list to locate all instances would fail to notice the cloned object. By making the method protected, only objects of the same class (or a subclass) can produce a clone and implementers of those classes are (presumably) aware of the appropriate invariants and can arrange for them to be true without necessarily calling a constructor.

Example

The following example shows a class called MyClass as well as a representation of the instance of MyClass returned by object.MemberwiseClone .

C# Example

using System;
class MyBaseClass {
   public static string CompanyName = "My Company";
   public int age;
   public string name;
}

class MyDerivedClass: MyBaseClass {

   static void Main() {
   
   //Create an instance of MyDerivedClass
   //and assign values to its fields.
   MyDerivedClass m1 = new MyDerivedClass();
   m1.age = 42;
   m1.name = "Sam";

   //Do a shallow copy of m1
   //and assign it to m2.
   MyDerivedClass m2 = (MyDerivedClass) m1.MemberwiseClone();
   }
}

A graphical representation of m1 and m2 might look like this

Example

+---------------+

|     42        |                           m1 

+---------------+

|     +---------|-----------------> "Sam" 

+---------------+                    /|\ 

                                      | 

+---------------+                     | 

|     42        |                     |      m2 

+---------------+                     | 

|      +--------|---------------------| 

+---------------+

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0