空条件运算符评估为bool不为bool吗?符合预期 [英] Null-conditional operator evaluates to bool not to bool? as expected

查看:37
本文介绍了空条件运算符评估为bool不为bool吗?符合预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚从VS 2010升级到2015.我喜欢新的

I've just upgraded from VS 2010 to 2015. I like the new null-conditional operator which is also known as null-propagation. This enables to simplify your code, for example:

string firstCustomerName = customers?[0].Name; // null if customers or the first customer is null

另一个:

int? count = customers?[0]?.Orders?.Count();  // null if customers, the first customer, or Orders is null

即使 Enumerable.Count 返回 int 来区分有效计数和任何

返回 Nullable< int> null 之前.这是非常直观且非常有用的.

which returns a Nullable<int> even if Enumerable.Count returns an int to differentiate between a valid count and any nulls before. That's quite intuitive and very useful.

但是为什么要这样编译并按预期工作(返回 false ):

But why does this compile and work as expected (it returns false):

string text = null;
bool contains = text?.IndexOf("Foo", StringComparison.CurrentCultureIgnoreCase) >= 0;

它应该返回 bool?(不是)或不编译.

It should either return bool? (which it does not) or not compile.

推荐答案

您真正拥有的是

string text = null;
int? index = text?.IndexOf("Foo", StringComparison.CurrentCultureIgnoreCase);
bool contains = index >= 0;

int?> = int 是完全合法的.

在那里拆分的原因是操作员的文档指出"如果条件成员访问和索引操作链中的一个操作返回null,则链中其余部分的执行将停止.表达式中优先级较低的其他操作将继续."表示.?将仅在创建值"之前评估具有相同优先级或更高优先级的事物.

The reason it was split there is the documentation for the operator states "If one operation in a chain of conditional member access and index operation returns null, then the rest of the chain’s execution stops. Other operations with lower precedence in the expression continue." That means .? will only evaluate things with the same precedence or higher before it "creates a value".

如果您查看运算符优先级的顺序,您将看到关系和类型-testing运算符"在列表中要低得多,因此将在应用> = 之前创建该值.

If you look at the order of operator precedence you will see that "Relational and Type-testing Operators" are much lower in the list so the value will be created before the >= is applied.

更新:由于它是在注释中提出的,因此这是C#5规范部分,介绍> = 和其他运算符在处理可空值时的行为价值.我找不到C#6的文档.

UPDATE: Because it was brought up in the comments, here is the C# 5 spec section on how the >= and other operators behave when dealing with a nullable value. I could not find a document for C# 6.

7.3.7解除操作员

提升的运算符允许对非空值类型进行操作的预定义和用户定义运算符也可与可空值一起使用这些类型的形式.提升操作员是根据预定义的以及满足某些要求的用户定义的运算符,例如描述如下:

7.3.7 Lifted operators

Lifted operators permit predefined and user-defined operators that operate on non-nullable value types to also be used with nullable forms of those types. Lifted operators are constructed from predefined and user-defined operators that meet certain requirements, as described in the following:

  • 对于一元运算符
    + ++--!〜

如果操作数和结果类型均为非空值类型,则存在运算符的提升形式.抬起的形式是通过添加单个构造?操作数和结果的修饰符类型.如果操作数是无效的.否则,提升的运算符将解开操作数,并应用基础运算符,并包装结果.

a lifted form of an operator exists if the operand and result types are both non-nullable value types. The lifted form is constructed by adding a single ? modifier to the operand and result types. The lifted operator produces a null value if the operand is null. Otherwise, the lifted operator unwraps the operand, applies the underlying operator, and wraps the result.

对于二进制运算符
+-*/%&|^<<>>

如果操作数和结果类型存在,则存在提升形式的运算符都是非空值类型.抬起的表格是由加一个?每个操作数和结果类型的修饰符.解除如果一个或两个操作数为null(一个是&和|布尔运算符?类型,如前所述在第7.11.3节中).否则,提升后的运算符将取消包装操作数,应用基础运算符,然后包装结果.

a lifted form of an operator exists if the operand and result types are all non-nullable value types. The lifted form is constructed by adding a single ? modifier to each operand and result type. The lifted operator produces a null value if one or both operands are null (an exception being the & and | operators of the bool? type, as described in §7.11.3). Otherwise, the lifted operator unwraps the operands, applies the underlying operator, and wraps the result.

对于相等运算符
==!=

如果操作数类型都是两种,则存在提升形式的运算符不可为null的值类型,并且结果类型为bool.解除表格是通过添加单个?来构造的.每个操作数的修饰符类型.提升运算符认为两个null值相等,并且null值不等于任何非空值.如果两个操作数都不为空,被解除的运算符解开操作数并应用基础运算符产生布尔结果.

a lifted form of an operator exists if the operand types are both non-nullable value types and if the result type is bool. The lifted form is constructed by adding a single ? modifier to each operand type. The lifted operator considers two null values equal, and a null value unequal to any non-null value. If both operands are non-null, the lifted operator unwraps the operands and applies the underlying operator to produce the bool result.

对于关系运算符
<>< => =

如果操作数类型均为非空值类型,并且结果类型为bool,则存在提升形式的运算符.解除表格是通过添加单个?来构造的.每个操作数的修饰符类型.如果一个或两个都被提升,则运算符将产生false值操作数为空.否则,提升的运算符将取消包装操作数并应用基础运算符以产生布尔结果.

a lifted form of an operator exists if the operand types are both non-nullable value types and if the result type is bool. The lifted form is constructed by adding a single ? modifier to each operand type. The lifted operator produces the value false if one or both operands are null. Otherwise, the lifted operator unwraps the operands and applies the underlying operator to produce the bool result.

这篇关于空条件运算符评估为bool不为bool吗?符合预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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