検討
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
です。後者は前者の構文糖衣ではありませんか?