Microsoft 更喜欢 False 值?
我正在读乔恩·斯基特的书。 (#4)
但有一件事(除其他外)引起了我的注意:
主题:布尔?
他在一张表中写道:(X,Y 是布尔值?)
X | Y | X & Y
---------------------------
true null null
好吧……所以 null 是决定者。 这里的 bool 操作数丢失了。
X | Y | X & Y
---------------------------
false null false
为什么? 为什么这里要考虑 bool 操作数,而在前面的示例中,是 null 决定结果......?
看来真假有朋友在不同的地方......:)
Im reading Jon Skeet book. (#4)
but one thing (among others) caught my eye :
topic : bool?
he wrote in a table that :(X,Y are bool?)
X | Y | X & Y
---------------------------
true null null
ok fine... so the null is the one who decides.
the bool operand here looses.
X | Y | X & Y
---------------------------
false null false
why ?
why the bool operand here is being taking into account while in the previous sample it was the null who decided about the result ...?
It seems that true and false has friends in different places....:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将null发音为未知。
当然还有 OR 的镜像表:
它也适用于
&&
和||
。Pronounce null as unknown.
And of course there is the mirror table for OR :
And it holds for
&&
and||
as well.如此设计的原因是为了与实现的三值逻辑保持一致在许多其他平台上,包括 SQL。在 Kleene 逻辑(三值逻辑所基于的)中,
TRUE AND UNKNOWN
给出UNKNOWN
,而FALSE AND UNKNOWN
给出FALSE
。您可以参考标题为“布尔值? 使用可空类型(C# 编程指南) 中的“type”有关 C# 中结果的解释和枚举。
The reason it was designed that way is to be consistent with three-valued logic, as implemented in numerous other platforms, including SQL. In Kleene logic (on which three-valued logic is based),
TRUE AND UNKNOWN
givesUNKNOWN
, whileFALSE AND UNKNOWN
givesFALSE
.You may refer to the section titled “The bool? type” within Using Nullable Types (C# Programming Guide) for an explanation and enumeration of the results in C#.
根据微软的说法,这是...
按位运算的真值表可以在此页面上找到可为 null 的布尔值。
According to Microsoft, it is...
The truth table for bitwise operations on nullable booleans can be found on this page.
原因是您现在正在处理三值逻辑。
因此,您列出的表格并不完整:您需要更多条目来查看何时会发生什么,因为有 8 种可能的输入组合(如果算上
null 运算符 null
组合,则有 9 种)。AND:
OR:
使用您的
朋友
类比,您可以说,对于三值逻辑,AND 倾向于 FALSE 和 NULL 作为结果,而 OR 倾向于 TRUE 和 NULL。The reason is that you are now dealing with three-valued logic.
So the tables you list are incomplete: you need more entries to see what happens when, as there are 8 possible input combinations (nine if you count the
null operator null
combination).AND:
OR:
Using your
friends
analogy, you could say that, for three-valued logic, AND favours FALSE and NULL as outcome whereas OR favours TRUE and NULL.&&和 ||运营商均根据短路评估进行评估。这意味着一旦确定复杂表达式为假,则不会检查其余表达式。由于第二个表达式中的假值足以确定逻辑与的结果,因此直接将结果确定为假。
The && and || operators are both evaluated according to short circuit evaluation. This means once a complex expression has been determined to be false, the remaining expressions will not be checked. Since false value in second expression is enough to determine the result of logical AND, result is determined as false directly.