这个让我感到沮丧,所以我想我会在这里问,希望C#guru可以向我解释.
为什么这段代码会产生错误?
class Program
{
static void Main(string[] args)
{
Foo(X); // the error is on this line
}
static String X() { return "Test"; }
static void Foo(Func<IEnumerable<String>> x) { }
static void Foo(Func<String> x) { }
}
Run Code Online (Sandbox Code Playgroud)
有问题的错误:
Error
1
The call is ambiguous between the following methods or properties:
'ConsoleApplication1.Program.Foo(System.Func<System.Collections.Generic.IEnumerable<string>>)' and 'ConsoleApplication1.Program.Foo(System.Func<string>)'
C:\Users\mabster\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs
12
13
ConsoleApplication1
Run Code Online (Sandbox Code Playgroud)
我使用的是什么类型并不重要 - 如果在该代码中用"int"替换"String"声明,您将得到相同类型的错误.这就像编译器不能告诉之间的区别Func<T>和Func<IEnumerable<T>>.
有人可以对此有所了解吗?