I just wanted to know what is the use of IClonable interface in .NET?
Well, not much really. It earned a special mention in the Framework Design Guidelines as an interface to avoid.
Do not implement ICloneable. There are two general ways to implement ICloneable, either as a deep, or non-deep copy. Deep-copy copies the cloned object and all objects referenced by the object, recursively until all objects in the graph are copied. A non-deep copy (referred to as ‘shallow’ if only the top level references are copied) may do none, or part of a deep copy. Because the interface contract does not specify the type of clone performed, different classes have different implementations. A consumer cannot rely on ICloneable to let them know whether an object is deep-cloned or not.
There has been discussion in the past about obsoleting it. I am not sure what ever came of that, but the framework designers have admitted that it was probably a mistake.
If you want to support cloning then I would create and implement separate interfaces IDeepCopy
and IShallowCopy
or the like.
From MSDN: "The ICloneable interface contains one member, Clone, which is intended to support cloning beyond that supplied by MemberwiseClone."
It is an interface that if implemented signals that instances of the class be cloned and not only shallow copied. Implementation of IClonable interface does not say anything about if it shallow copied or deep copied, though.
There is an interesting discussion about how to use IClonable here: http://channel9.msdn.com/forums/TechOff/202972-IClonable-deep-vs-shallow-best-practise/ .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With