Cloning

We know that when we assign one reference-type object to another, both the objects point to the same memory location after the operation is completed. If a member field in one object is changed, for example, it gets reflected in the other object.

Sometimes you might want to clone the object, i.e., create a duplicate copy of the object. The .NET Framework formalizes this notion of cloning by meaning of a standard interface, ICloneable. Here is its definition:

public interface ICloneable {
     Object Clone();
}

The interface defines just one method, Clone. The purpose of this method is to return a clone of the object. Any class that implements ICloneable must implement Clone and the necessary logic to duplicate the object.

Interface ICloneable ...

Get .NET Programming: A Practical Guide Using C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.