Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to explain this behaviour with Overloaded and Overridden Methods? [duplicate]

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);
    }
}
like image 911
mic4ael Avatar asked May 12 '14 17:05

mic4ael


2 Answers

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.

like image 108
Sriram Sakthivel Avatar answered Oct 12 '22 23:10

Sriram Sakthivel


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).

like image 41
Ahmad Mageed Avatar answered Oct 12 '22 22:10

Ahmad Mageed