Why overloaded methods have lower priority than in

2019-03-18 22:45发布

I have base class A

public class A
{
    public virtual void Method(A parameter)
    {
        Console.WriteLine(MethodBase.GetCurrentMethod());
    }
    public virtual void Method(B parameter)
    {
        Console.WriteLine(MethodBase.GetCurrentMethod());
    }
}

Inhereted B

public class B : A
{
    public virtual void Method(object parameter)
    {
        Console.WriteLine(MethodBase.GetCurrentMethod());
    }

    public override void Method(A parameter)
    {
        Console.WriteLine(MethodBase.GetCurrentMethod());
    }

    public override void Method(B parameter)
    {
        Console.WriteLine(MethodBase.GetCurrentMethod());
    }
}

Static class S with extension method

public static class S
{
    public static void Method(this B instance, B parameter)
    {
        Console.WriteLine(MethodBase.GetCurrentMethod());
    }
}

Example we create instance of type B and invoke Method on it, we expect that it will be public override void Method(B parameter) actual result is public virtual void Method(object parameter).

var b = new B();
b.Method(new B()); // B.Method (Object parameter) Why???

Why compiler doesn't select more suitible method??? UPD And why it is not extension method?

1条回答
forever°为你锁心
2楼-- · 2019-03-18 23:02

Why compiler doesn't select more suitible method?

Because it's following the rules of the language specification, where any candidate methods originally declared in a base class (over if they're overridden in a derived class) are ignored when finding a candidate method, unless the derived class doesn't have any applicable methods, at which point the search moves up to the base class, etc.

This is designed to avoid the "brittle base class" problem, but I find that hard to swallow in the face of the method being overridden in the derived class.

The relevant bit of the C# 4 specification is 7.4, which ends with this:

For member lookups in types other than type parameters and interfaces, and member lookups in interfaces that are strictly single-inheritance (each interface in the inheritance chain has exactly zero or one direct base interface), the effect of the lookup rules is simply that derived members hide base members with the same name or signature.

EDIT: About extension methods...

And why it is not extension method?

From section 7.6.5.2 of the spec:

In a method invocation (§7.5.5.1) of one of the forms

expr . identifier ( )
expr . identifier ( args )
expr . identifier < typeargs > ( )
expr . identifier < typeargs > ( args )

if the normal processing of the invocation finds no applicable methods, an attempt is made to process the construct as an extension method invocation

So an extension method is only used as a last resort, basically.

查看更多
登录 后发表回答