Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
System

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
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)

Visual Basic (Declaration)
<ComVisibleAttribute(True)> _
Public Interface ICloneable
Visual Basic (Usage)
Dim instance As ICloneable
C#
[ComVisibleAttribute(true)] 
public interface ICloneable
C++
[ComVisibleAttribute(true)] 
public interface class ICloneable
J#
/** @attribute ComVisibleAttribute(true) */ 
public interface ICloneable
JScript
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
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Don't implement this interface.      Jonathan Allen   |   Edit   |   Show History

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.


 

RE: Don't implement this interface.      createdbyx   |   Edit   |   Show History

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. 

RE: Don't implement this interface.      wekempf   |   Edit   |   Show History

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.

The use of this interface      Tony Blair78   |   Edit   |   Show History
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;
Tags What's this?: Add a tag
Flag as ContentBug
RE: The use of this interface      Neil Dodson   |   Edit   |   Show History

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.

Tags What's this?: Add a tag
Flag as ContentBug
A type-safe pattern to implement this interface      Kristof Verbiest   |   Edit   |   Show History
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
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker