法推理不与方法组工作 [英] Method Inference does not work with method group

查看:181
本文介绍了法推理不与方法组工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑

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错误的描述是

The description for the CS0121 error is

调用不明确以下方法或属性之间:   <$c$c>'System.Linq.Enumerable.Sum<string>(System.Collections.Generic.IEnumerable<string>,   System.Func&LT;字符串,小数&GT;)和   <$c$c>'System.Linq.Enumerable.Sum<string>(System.Collections.Generic.IEnumerable<string>,   System.Func&LT;字符串,小数&GT; )

The call is ambiguous between the following methods or properties: 'System.Linq.Enumerable.Sum<string>(System.Collections.Generic.IEnumerable<string>, System.Func<string,decimal>)' and 'System.Linq.Enumerable.Sum<string>(System.Collections.Generic.IEnumerable<string>, System.Func<string,decimal?>)'

我不明白的是,什么样的信息呢 S =&GT;的GetValue(S)给编译器,简单地的GetValue 不 - ?是不是后者语法糖为前

What I don't understand is, what information does s => GetValue(s) give the compiler that simply GetValue doesn't - isn't the latter syntactic sugar for the former ?

推荐答案

标记的答案是正确的,但可以使用更多的解释。

Mark's answer is correct but could use a bit more explanation.

这个问题确实是由于怎样的方法组的处理,以及如何lambda表达式的处理方式之间存在微妙的差别。

The problem is indeed due to a subtle difference between how method groups are handled and how lambdas are handled.

具体,细微的不同之处在于的方法组被认为是转换为委托类型只在基础是否在参数的比赛,不是也是否在基础上返回类型的匹配。的lambda表达式检查双方的观点和返回值类型。

Specifically, the subtle difference is that a method group is considered to be convertible to a delegate type solely on the basis of whether the arguments match, not also on the basis of whether the return type matches. Lambdas check both the arguments and the return type.

这样做的原因奇怪的规则是方法组转换为代表基本上的的重载的问题的解决方案。假设D是委托类型双D(字符串s),M是含有方法,需要一个字符串,并返回一个字符串的方法组。当解析给M到D转换的意义,我们确实超载的分辨率,如果你说的M(串)。重载会选择接受一个字符串,并返回一个字符串的男,所以M是转换到委托类型的即使转换将导致错误后的。就像你说的常规重载会成功字符串s = M(空); - 重载解析成功,尽管这会导致转换失败后。

The reason for this odd rule is that method group conversions to delegates are essentially a solution of the overload resolution problem. Suppose D is the delegate type double D(string s) and M is a method group containing a method that takes a string and returns a string. When resolving the meaning of a conversion from M to D, we do overload resolution as if you had said M(string). Overload resolution would pick the M that takes a string and returns a string, and so M is convertible to that delegate type even though the conversion will result in an error later. Just as "regular" overload resolution would succeed if you said "string s = M(null);" -- overload resolution succeeds, even though that causes a conversion failure later.

这规则是微妙的,有点不可思议。它的结果这里就是你的方法组转换为的所有的不同的委托类型的是第二个参数的每一个版本的总和,需要一个委托的。由于没有最好的转换,可以发现,在方法组总和重载决议是不明确的。

This rule is subtle and a bit weird. The upshot of it here is that your method group is convertible to all the different delegate types that are the second arguments of every version of Sum that takes a delegate. Since no best conversion can be found, the overload resolution on method group Sum is ambiguous.

方法组转换规则是合理的,但有些奇怪的C#。我有点烦恼,他们不是用更多的直觉正确的拉姆达转换是一致的。

Method group conversions rules are plausible but a bit odd in C#. I am somewhat vexed that they are not consistent with the more "intuitively correct" lambda conversions.

这篇关于法推理不与方法组工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆