开发者

How does the compiler choose which method to call when a parameter type is ambiguous?

开发者 https://www.devze.com 2022-12-28 09:53 出处:网络
I have th开发者_如何学Pythone following code: [TestMethod] public void TestFoo() { Foo(null); } private void Foo (object bar)

I have th开发者_如何学Pythone following code:

    [TestMethod]
    public void TestFoo()
    {
        Foo(null);
    }

    private void Foo (object bar)
    {
        Console.WriteLine("Foo - object");
    }

    private void Foo (string bar)
    {
        Console.WriteLine("Foo - string");
    }

and when I run the test "TestFoo()", the console output is "Foo - string". How does the compiler decide which method to call?


It applies the "better conversion" rules (7.4.3.3 of the C# 3 spec) as part of overload resolution (section 7.4.3 in general).

Basically in this case there's a conversion from string to object, but not from object to string. Following the rules, that means the conversion from null to string is better than the one from null to object, so the overload with the string parameter is used.

Overload resolution can get extremely complicated when the following factors get involved:

  • There could be generic methods in the candidate set
  • If there are generic methods, type inference is applied to each of them separately, giving different conversion opportunities
  • If any arguments are method groups, they could be converted to different delegate types - possibly even using different method signatures if the named method group also has multiple overloads
  • Inheritance can lead to surprising results
  • Parameter arrays (params) add to the fun
  • Optional parameters in C# 4 contribute to the decision too

Basically overloading can be a real can of worms - where possible, design overloads so that only one of them will ever be a valid target of any given method call, so that you don't need to worry about the detailed rules.

0

精彩评论

暂无评论...
验证码 换一张
取 消