【发布时间】:2011-12-06 10:44:13
【问题描述】:
考虑
void Main()
{
var list = new[] {"1", "2", "3"};
list.Sum(GetValue); //error CS0121
list.Sum(s => GetValue(s)); //works !
}
double GetValue(string s)
{
double val;
double.TryParse(s, out val);
return val;
}
CS0121错误的描述是
以下方法或属性之间的调用不明确:
'System.Linq.Enumerable.Sum<string>(System.Collections.Generic.IEnumerable<string>, System.Func<string,decimal>)'和'System.Linq.Enumerable.Sum<string>(System.Collections.Generic.IEnumerable<string>, System.Func<string,decimal?>)'
我不明白的是,s => GetValue(s) 为编译器提供了哪些信息,而只是 GetValue 没有 - 后者不是前者的语法糖吗?
【问题讨论】:
标签: c# .net-4.0 type-inference method-group