In the given example (sorry for code style, it was in the interest of a compact self-contained repro), the method called is always the virtual method in B that has the additional integer parameter, while I would expect that the method implemented in C
would be the one called.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MethodChoosing
{
class Program
{
static void Main(string[] args)
{
C c = new C();
c.M();
Console.ReadKey();
}
}
public class A
{
public virtual void M(bool? a = null)
{
Console.WriteLine("base base");
}
}
public class B : A
{
public override void M(bool? a = null)
{
Console.WriteLine("base override");
}
public virtual void M(bool? a = null, int? b = null)
{
Console.Write("base new");
}
}
public class C : B
{
public override void M(bool? a = null)
{
Console.WriteLine("pick me!");
}
}
}
Which outputs "base new". This is obviously not the behaviour I expected, is anyone able to explain the reasoning?
Edit:
Changing main so that c.M
has a true or null parameter still selects the incorrect method.