I want to do contravariance
in override virtual method which implements from interface, I had try both ICloneTo<in T>
and ICloneTo<out T>
all getting compile errors, as the following code:
interface ICloneTo<T> {
void CloneTo(T obj);
}
abstract class Base : ICloneTo<Base> {
public string BaseProperty { get; set; }
public virtual void CloneTo(Base obj) {
obj.BaseProperty = BaseProperty;
}
}
class A : Base {
public string AProperty { get; set; }
public override void CloneTo(A obj /* Contravariance to A */) {
base.CloneTo(obj);
obj.AProperty = AProperty;
}
}