2 out of 17 rated this helpful - Rate this topic

ICloneable Interface

Supports cloning, which creates a new instance of a class with the same value as an existing instance.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

[ComVisibleAttribute(true)] 
public interface ICloneable
/** @attribute ComVisibleAttribute(true) */ 
public interface ICloneable
ComVisibleAttribute(true) 
public interface ICloneable

The ICloneable interface contains one member, Clone, which is intended to support cloning beyond that supplied by MemberwiseClone.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Re: the use of this interface
I was looking for something else, and stumbled upon this discussion thread, I agree with Tony Blaire78, it is exactly same what I have implemented when ever there was a need. With that said, I do believe that Microsoft should make the type of copy clear; i.e. it is Shallow or Deep upfront.
A type-safe pattern to implement this interface
The returned object is always of type 'System.Object' which is not type-safe. A type-safe pattern to implement this interface can be found here: http://kristofverbiest.blogspot.com/2008/10/type-safe-pattern-to-implement.html
RE: The use of this interface

MemberwiseClone is a protected method inherited from Object, whereas IClonable.Clone is by definition public because it's a member of an interface.

So isn't it the case that the two methods are used for quite different purposes - the former in some kind of internal operation by a class, and the latter where you call externally into the object and you either don't want to have to know which type is implementing the interface, or you don't want to have to know what implementation the IClonable is providing.

The use of this interface
It seems to me that the point of this interface has been obscured because the folks who named weren't sure if it will be used for a Deep or Shallow copy, so they took the safe side and said you can use it for both!.
However, if we just read the first line it looks like the there already exists "something" which creates a Shallow Copy of a reference value type object, namely Object.MemberwiseClone().

So if Object.MemberwiseClone() exists, then what is the point of ICloneable?
Yes, that's right ... it serves as placeholder for any developer who feels that his class needs to provide the facility of a Deep Copy. Deep Copy is defined as the exact replication of the object in the heap and not the just the references (or memory pointers).

To conclude, to me there is no confusion.

You want a Shallow Copy:
MyObject newObj = oldObj.MemberwiseClone() as MyObject;

You want a Deep Copy:
Implement ICloneable, then do
MyObject newObj = oldObj.Clone() as MyObject;
RE: Don't implement this interface.

Well, I have to disagree with David M. Kean about using copy constructors.  Not that I think copy constructors are bad (they aren't), but his argument fails on two grounds.

 

1.  A copy constructor has the exact same issue talked about by Jonathan.  There's no way to know if the copy constructor is going to make a deep or shallow copy.

 

2. A copy constructor can not be used polymorphically, which is the whole point in using a "Clone" method in OO languages.

Personally, I don't think throwing ICloneable out is appropriate.  Me, I always assume this does a shallow copy (which usually won't cause issues even if a deep copy is made), and if I must have a deep copy I use a custom IDeepCloneable interface.

The design of this interface was flawed, since it didn't specify the type of copy that would be done, but the concept is still useful and even necessary at times.

RE: Don't implement this interface.

I have to dissagree with Jonathan Allen's post regarding his "Don't implement this interface." argument.

The IClonable interface is usefull when used properly. For example in a business solution, specifics regarding shallow or deep clones would be laid out in the design doc's prior to implementation. So any object implementing the IClonable interface would/should adhere to those design specifications. 

Don't implement this interface.

This interface has been pretty much abandoned by Microsoft.

The problem is that it doesn't indicate if this is a shallow or deep clone. Nor does it give a way to indicate which is desired in the given circumstance.