Let's assume there is such a class hierarchy:
public class Parent
{
public virtual void Fun()
{
Console.WriteLine("In Parent");
}
}
public class FirstChild : Parent
{
public override void Fun()
{
Console.WriteLine("In FirstChild");
}
}
public class SecondChild : FirstChild
{
public new void Fun()
{
Console.WriteLine("In SecondChild");
}
}
Then we define a generic method which accepts argument derived from Parent:
public static void Test<T>(T entity) where T : Parent
{
entity.Fun();
}
Finally in a client code we call it:
SecondChild child = new SecondChild();
Test(child); // Displays "In FirstChild"
The question is why the Fun method from FirstChild is called instead of Fun method defined in SecondChild?
Is there a casting involved behind the scenes?