【发布时间】:2011-01-01 02:34:07
【问题描述】:
这个让我很困惑,所以我想我会在这里问,希望 C# 大师可以向我解释。
为什么这段代码会产生错误?
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) { }
}
有问题的错误:
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
我使用什么类型并不重要——如果你用“int”替换代码中的“String”声明,你会得到同样的错误。就像编译器无法区分Func<T>和Func<IEnumerable<T>>一样。
有人能解释一下吗?
【问题讨论】: