Different behaviour of method overloading in C#

后端 未结 3 898
故里飘歌
故里飘歌 2020-12-09 11:14

I was going through C# Brainteasers (http://www.yoda.arachsys.com/csharp/teasers.html) and came across one question: what should be the output of this code?

         


        
3条回答
  •  无人及你
    2020-12-09 11:37

    It's because the methods signatures in the derived class are matched first before considering the base class signatures. Therefore, when you try:

    d.Foo(i);
    

    It attempts to match the method signature against your current class (not the base class). It finds an acceptable match Foo(object) and doesn't further consider the base class method signatures.

    It's all about the compiler finding a matching method signature, and the search order it uses to find these signatures.

    -Doug

提交回复
热议问题