Could anyone be so nice and explain me why this code shows Derived.DoWork(double)
. I can come up with some explanations for this behaviour, however I want someone to clarify this for me.
using System;
public class Base
{
public virtual void DoWork(int param) {
Console.WriteLine("Base.DoWork");
}
}
public class Derived : Base
{
public override void DoWork(int param) {
Console.WriteLine("Derived.DoWork(int)");
}
public void DoWork(double param) {
Console.WriteLine("Derived.DoWork(double)");
}
public static void Main() {
int val = 5;
Derived d = new Derived();
d.DoWork(val);
}
}
Eric lippert used to say always "Closer is better".
A method first declared in a derived class is closer than a method first declared in a base class.
So from the above link, derived class is closer hence that is chosen.
This behavior is carefully implemented to avoid Brittle base class problem
For completeness I'll share the bullets:
A method first declared in a derived class is closer than a method first declared in a base class.
A method in a nested class is closer than a method in a containing class.
Any method of the receiving type is closer than any extension method.
An extension method found in a class in a nested namespace is closer than an extension method found in a class in an outer namespace.
An extension method found in a class in the current namespace is closer than an extension method found in a class in a namespace mentioned by a using directive.
An extension method found in a class in a namespace mentioned in a using directive where the directive is in a nested namespace is closer than an extension method found in a class in a namespace mentioned in a using directive where the directive is in an outer namespace.
This behavior is defined in the the C# Language Specification, specifically section 7.5.3 "Overload resolution." Here's a link to an older version, otherwise refer to the CSharp Language Specification.docx that you should have locally, e.g., C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC#\Specifications\1033\CSharp Language Specification.docx.
In this case, methods marked override are excluded, thus the double
overload is the only valid option (emphasis mine):
Each of these contexts defines the set of candidate function members and the list of arguments in its own unique way, as described in detail in the sections listed above. For example, 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).
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