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,这意味着它具有足够的信息来解决歧义。
我会理解,如果编译器没有在重载解析期间查看返回值,因此两个调用都失败了,但是我的问题是,为什么一个调用编译正常,而另一个调用没有编译正常?