I understand the difference between a deep and shallow clone of an object, but according to Simon's answer on this (copy-constructor-versus-clone) question, a generic and non-generic version should be supplied. Why?
You can define two interfaces, one with a generic parameter to support strongly typed cloning and one without to keep the weakly typed cloning ability for when you are working with collections of different types of cloneable objects:
I mean it's trivial enough to make the different interfaces, but in the generic-heavy paradigm of modern C#, I am finding it difficult to come up with a valid reason why you would ever want to use the non-generic and weakly-typed version. Heck, you can even have T:object
and do the same thing!
I would write my interfaces like this:
public interface IShallowCloneable
{
object Clone();
}
public interface IShallowCloneable<T> // Should this derive IShallowCloneable?
{
T Clone();
}
public interface IDeepCloneable
{
object Clone();
}
public interface IDeepCloneable<T> // Should this derive IDeepCloneable?
{
T Clone();
}
And my class would implement it like this:
public class FooClass : IDeepCloneable<FooClass>
{
// Implementation
}