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

佐手、 提交于 2019-12-30 03:03:56

问题


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);
    }
}

回答1:


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.




回答2:


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




回答3:


This behavior is apparently by design:

'When choosing an overload, if there are any compatible methods declared in a derived class, all signatures declared in the base class are ignored - even if they're overridden in the same derived class!' http://social.msdn.microsoft.com/Forums/vstudio/en-US/a70b25d4-f310-4d06-9dc2-a453f822f4f3/function-not-getting-called-when-overloading-the-function-of-base-class-in-derived-class-with?forum=csharpgeneral



来源:https://stackoverflow.com/questions/23615356/how-to-explain-this-behaviour-with-overloaded-and-overridden-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!