The ICloneable
interface by itself isn't very useful, which is to say that there really aren't many situations where it's useful to know that an object is cloneable without knowing anything else about it. This is a very different situation from e.g. IEnumerable
or IDisposable
; there are many situations where it's useful to accept an IEnumerable
without knowing anything other than how to enumerate it.
On the other hand, ICloneable
may be useful when applied as a generic constraint along with other constraints. For example, a base class might usefully support a number of derivatives, some of which could be usefully cloned, and some of which could not. If the base type itself exposed a public cloning interface, then any derivative type which could not be cloned would violate the Liskov Substitution Principle. The way to avoid this problem is to have the base type support cloning using a Protected method, and allow derived types to implement a public cloning interface as they see fit.
Once that was accomplished, a method which wants to accept an object of a WonderfulBase
type, and needs to be able to clone it, could be coded to accept a WonderfulBase object which supports cloning (using a generic type parameter with base-type and ICloneable
constraints). Although the ICloneable
interface would not itself indicate deep or shallow cloning, the documentation for WonderfulBase
would indicate whether cloneable WonderfulBase
should be deep- or shallow-cloned. Essentially, the ICloneable
interface wouldn't accomplish anything that wouldn't be accomplished by defining ICloneableWonderfulBase
, except that it would avoid having to define different names for every different cloneable base class.