Well, you may need to do it; here is a possible scenario:
When cloning, we usually want to return not universal object
by cloned object type:
class Base {
// We can't declare this method as virtual: Derived class will return Derived
public Base Clone() {
...
}
}
...
Base test = new Base();
Base duplicate = test.Clone();
If we want to implement IClonable
we have to do it explictly (in order to solve Clone()
names conflict):
class Base : ICloneable {
public Base Clone() {
...
}
// This method is efficiently private, and that's why can't be virtual
object ICloneable.Clone() => Clone();
}
Now we want a Derived
class which can be cloned as well:
class Derived : Base {
// Note that now we have new Clone method which returns Derived instance (not Base one)
public new Derived Clone() {
...
}
}
...
Derived test = new Derived();
Derived duplicate = test.Clone();
If we keep it like this then we'll have wrong behaviour:
Derived test = new Derived();
// This will be wrong:
// Base.ICloneable.Clone() will be called which executes "Base Clone()" method
// instead of expected "new Derived Clone()"
object clone = (test as IClonable).Clone();
So we have to reimplement IClonable
interface:
class Derived : Base, ICloneable {
// Please, note that now we have new Clone method which returns Derived instance
public new Derived Clone() {
...
}
// This ICloneable implementation will call Derived Clone()
object ICloneable.Clone() => Clone();
}
...
// Derived ICloneable.Clone() will be called
// which executes "new Derived Clone()" method
object clone = (test as IClonable).Clone();