O'Reilly logo

C# in a Nutshell by Peter Drayton, Ted Neward, Ben Albahari

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

ICloneable

Synopsis

ICloneable is a marker interface, indicating that an object can be cloned (that is, have a completely identical copy created). It consists of a single method, Clone(), which is called by clients wishing to create a complete copy of the ICloneable-implementing class.

When speaking of cloning, the terms deep copy cloning and shallow copy cloning indicate how deeply into the object graph a clone operation will carry itself. A deep copy not only clones the object called, but in turn seeks to clone any objects to which it holds reference. This sort of operation must be handled by the programmer, usually by calling Clone() in turn on each object this object references. A shallow copy is a complete bitwise copy of this object; any objects referenced by this object are also be referenced by the cloned object.

The simplest way to implement a shallow clone is to use the Object.MemberwiseClone() method to copy this object’s fields directly and then return. A deep clone also calls MemberwiseClone(), but then also asks each object reference held within this object to Clone() itself.

public interface ICloneable {
// Public Instance Methods
   public method object Clone();  
}

Implemented By

Multiple types

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required