Extension methods overloading in C#, does it work?

后端 未结 5 765
遥遥无期
遥遥无期 2020-12-11 17:27

Having a class that has a method, like this:

class Window {
    public void Display(Button button) {
        // ...
    }
}

is it possible

5条回答
  •  悲哀的现实
    2020-12-11 17:46

    That should work, the compiler will almost always pick an instance method with an acceptable signature over an extension method with the exact signature.

    According to this article:

    An instance method with an acceptable signature using widening conversion will almost always be preferred over an extension method with an exact signature match. If this results in binding to the instance method when you really want to use the extension method, you can explicitly call the extension method using the shared method calling convention. This is also the way to disambiguate two methods when neither is more specific.

    Are you sure you're explicitly passing a Button?

    Or is void Display(Button button) recursively calling itself?

提交回复
热议问题