相关疑难解决方法(0)

方法推断不适用于方法组

考虑

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;
}
Run Code Online (Sandbox Code Playgroud)

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# type-inference .net-4.0 method-group

11
推荐指数
2
解决办法
1762
查看次数

标签 统计

.net-4.0 ×1

c# ×1

method-group ×1

type-inference ×1