I have simplified my code to this
internal class Program
{
private static void Main(string[] args)
{
Child d = new Child();
int i = 100;
d.AddToTotal(i);
Console.ReadKey();
}
private class Parent
{
public virtual void AddToTotal(int x)
{
Console.WriteLine("Parent.AddToTotal(int)");
}
}
private class Child : Parent
{
public override void AddToTotal(int number)
{
Console.WriteLine("Child.AddToTotal(int)");
}
public void AddToTotal(double currency)
{
Console.WriteLine("Child.AddToTotal(double)");
}
}
}
The issue is that this calls
public void AddToTotal(double currency)
although I am calling it with an int and it should be using
public override void AddToTotal(int number)
Using the parent returns the expected result.
Parent d = new Child();
int i = 100;
d.AddToTotal(i);
Update:
Thanks to @Jan and @azyberezovsky for pointing me to the specification. I have added a virtual empty method to the base class to get around this for now.
A member lookup of a name N in a type T is processed as follows:
First, the set of all accessible members named N declared in T and the base types of T is constructed. Declarations that include an override modifier are excluded from the set. If no members named N exist and are accessible, then the lookup produces no match, and the following steps are not evaluated.
Thus when you use variable of Child type
method
public override void AddToTotal(int number)is excluded from set, and we have only one method with name N left. Int is converted implicitly to double, so no errors occured.