【发布时间】:2015-02-13 05:31:50
【问题描述】:
假设我有以下方法:
static int MethodWithDefaultParameters(int a, int b=0, int c=1)
{
return a + b + c;
}
然后我在 LINQ 查询中使用此方法,如下所示:
Enumerable.Range(1,10).Select(MethodWithDefaultParameters);
这失败了:
错误 1 无法从用法中推断方法“System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)”的类型参数。尝试明确指定类型参数。
当然,我可以通过插入一个转发函数调用的 lambda 来解决这个问题,如下所示:
Enumerable.Range(1,10).Select(i => MethodWithDefaultParameters(i));
但我的问题是为什么类型推断会失败?据我所知,它不应该是模棱两可的,因为只有一个函数的变体满足输入变量。
【问题讨论】:
标签: c# type-inference