I wonder what is the reason for the invocation of the method that prints "double in derived". I didn't find any clue for it in the C# specification.
public class A
{
public virtual void Print(int x)
{
Console.WriteLine("int in base");
}
}
public class B : A
{
public override void Print(int x)
{
Console.WriteLine("int in derived");
}
public void Print(double x)
{
Console.WriteLine("double in derived");
}
}
B bb = new B();
bb.Print(2);
Straight from the C# spec (7.5.3 Overload resolution):
the set of candidates for a method invocation does not include methods marked override (§7.4), and methods in a base class are not candidates if any method in a derived class is applicable (§7.6.5.1).
In your example, the overriden Print(int x)
is not a candidate and Print(double x)
is applicable, so it is picked without a need to consider the methods in the base class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With