条件运算符和比较委托 [英] Conditional operator and Comparison Delegate

查看:103
本文介绍了条件运算符和比较委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出比较方法的两种实现方式:

Given two implementations of Comparison methods:

// compares by Key...
private static int CompareByKey(KeyValuePair<int, string> x, KeyValuePair<int, string> y)

{
    return x.Key.CompareTo(y.Key);
}

// compares by Value...
private static int CompareByValue(KeyValuePair<int, string> x, KeyValuePair<int, string> y)
{
    return x.Value.CompareTo(y.Value);
}

为什么以下条件运算符代码块无法编译:

Why wouldn't the following conditional operator code block compile:

Comparison<KeyValuePair<int, string>> sortMethod;
sortMethod = isSortByActualValue ? CompareByKey : CompareByValue;

编译器错误:由于无法在'方法组'和'方法组'之间进行隐式转换,因此无法确定条件表达式的类型"

Compiler error: "Type of conditional expression cannot be determined because there is no implicit conversion between 'method group' and 'method group'"

但是,使用if-else的等效代码块没有任何问题:

However, the equivalent code block using if-else does not have any issue:

Comparison<KeyValuePair<int, string>> sortMethod;
if (isSortByActualValue)
    sortMethod = CompareByKey;
else
    sortMethod = CompareByValue;

(在以上两个作业中都很好)

(all good in both assignments above)

如果我强制比较比较委托,那么条件运算符也将如此:

So does the conditional operator, if I cast the Comparison delegate:

Comparison<KeyValuePair<int, string>> sortMethod;
sortMethod = isSortByActualValue ? (Comparison<KeyValuePair<int, string>>) CompareByKey : CompareByValue;

(在上面的分配中,即使仅在真实零件上进行铸造,这一切都很好)

(all good in the assignment above, when cast even though casting was only on the true part)

推荐答案

该错误方法实际上可以说明所有内容,但不是很直观.如果在不调用方法的情况下使用方法名称,则说明正在处理方法组. 组",因为方法可以重载,并且名称可以指示任何重载的方法.

Th error method actually says it all but it’s not quite intuitive. If you use a method name without invoking the method, you are handling a method group. "group", because a method could be overloaded and the name can indicate any of the overloaded methods.

现在,方法组可以隐式转换为具有匹配签名的委托,这就是为什么您在if中进行分配的原因.

Now, method groups are convertible implicitly to a delegate with matching signature, this is why your assignment in if works.

到目前为止,太好了.但是,条件运算符?:需要推断出可以将其第二个和第三个参数隐式转换为的通用类型,并且它不会考虑所有转换(这将有各种各样的问题).只是看两个参数是否具有相同的类型,或者一个参数是否可以隐式转换为另一个.

So far, so good. However, the conditional operator ?: needs to deduce a common type to which its second and third arguments can be implicitly converted, and it does not consider all conversions for that (this would have diverse problems). It merely looks whether both arguments have the same type, or whether one is implicitly convertible into the other.

这里不是这种情况:尽管两个参数都是方法组,但实际上它们是具有不同类型的不同方法组,并且您不能将一个方法组转换为另一个方法组.即使两者都可以很容易地转换为委托,编译器仍禁止这种用法.

This is not the case here: although both arguments are method groups, they are in fact different method groups with distinct types, and you cannot convert one method group into another. Even though both can readily be converted into a delegate, the compiler forbids this usage.

顺便说一句,其他类型也是如此:

The same is true for other types, by the way:

object = someBool ? "" : New List<Integer>();

出于相同的原因,

也无法编译.再一次,我们可以通过将任意一个参数显式转换为通用基本类型来进行编译:

also fails to compile, for the same reason. And again, we can make this compile by explicitly casting either of the arguments to a common base type:

object = someBool ? (object) "" : New List<Integer>();

这篇关于条件运算符和比较委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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