Why is this method invocation ambiguous?

eoi*_*lan 6 c# roslyn c#-7.2

Why does the first call to Foo below compile but the second one results in an ambiguous invocation compiler error?

(using c# 7.2)

    private static void AmbiguousAsyncOverload() {
      Foo(() => Bar());  // This is OK
      //Foo(Bar);        // Error, ambiguous overload
    }

    private static void Foo(Func<int> func) {
      func();
    }

    private static void Foo(Func<string> func) {
      func();
    }

    private static int Bar() {
      return 4;
    }
Run Code Online (Sandbox Code Playgroud)

如果我删除的first(Func<int>)实现Foo,并因此消除了歧义的可能性,则编译器(正确)报告Bar没有正确的签名要传递给Foo,这意味着它具有足够的信息来解决歧义。

我会理解,如果编译器没有在重载解析期间查看返回值,因此两个调用都失败了,但是我的问题是,为什么一个调用编译正常,而另一个调用没有编译正常?

Dav*_*idG 6

在v7.3中修复之前,这对于所有C#版本都是一个问题。重载解析期间未考虑返回类型。从C#7.3 的发行说明(或语言建议)中:

  1. 对于方法组转换,将从集合中删除其返回类型与委托的返回类型不匹配的候选方法。