If I have a base class
class Foo : IClonable {
public object virtual Clone(){ return new Foo(); }
}
and a child class incorrectly overriding clone with new
instead of override
. It's a simplification of a bug in a third party library I'm trying to work around.
class Bar : Foo {
public new Clone() { return new Bar(); }
}
and then I have two methods
public static T Bang(T source)
where T : Foo
{
return (T) source.Clone();
}
and
public static Bar Bang(Bar source)
{
return (Bar) source.Clone();
}
now if I invoke the first one with an instance of Bar
I get a Foo
back. If I invoke the second I get Bar
back.
I'm curious as to why the generic version doesn't get the new
version of Clone()
but rather the inherited version.
Is it that the type T
is erased after the constraints are met and then it just behaves using the base class?