Why does c# not call the new Method1
from DerivedClass and instead calls Method1 on the BaseClass?
using System;
class BaseClass
{
public void Method1()
{
Console.WriteLine("Base - Method1");
}
}
class DerivedClass : BaseClass
{
public new void Method1()
{
Console.WriteLine("Derived - Method1");
}
}
class GenericClass<T> where T: BaseClass {
public void CallMethod(T instT) {
instT.Method1();
}
}
public class Program
{
public static void Main()
{
DerivedClass dc = new DerivedClass();
GenericClass<DerivedClass> gc = new GenericClass<DerivedClass>();
gc.CallMethod(dc);
dc.Method1();
}
}
# outputs
Base - Method1
Derived - Method1
EDIT: The specification at 8.5 say this. It seems related.
The rules for member lookup on type parameters depend on the constraints, if any, applied to the type parameter. They are detailed in §11.5.