Microsoft Likes the False value better?

淺唱寂寞╮ 提交于 2019-12-23 14:24:46

问题


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....:)


回答1:


Pronounce null as unknown.

true  & unknown => unknown
false & unknown => false       because the second operand does not matter. 

And of course there is the mirror table for OR :

true  | unknown => true        because the second operand does not matter. 
false | unknown => unknown       

And it holds for && and || as well.




回答2:


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 gives UNKNOWN, while FALSE AND UNKNOWN gives FALSE.

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#.




回答3:


According to Microsoft, it is...

To ensure that the results produced by the & and | operators are consistent with the three-valued Boolean type in SQL

The truth table for bitwise operations on nullable booleans can be found on this page.




回答4:


The reason is that you are now dealing with three-valued logic.

  • for an AND operator (be it & or &&) the expression result can be determined at the first match for FALSE.
  • for an OR operator (be it | or ||) the expression result can be determined at the first match for TRUE.
  • for any boolean operation having a left most operand of NULL will return NULL.

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:

X      |    Y     |   X & Y
---------------------------
true       null        null
null       true        null
false      null       false
null       false       null

true       false      false
true       true        true
false      true       false
false      false      false

OR:

X      |    Y     |   X | Y
---------------------------
true       null        true
null       true        null
false      null        null
null       false       null

true       false       true
true       true        true
false      true        true
false      false      false

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.




回答5:


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.



来源:https://stackoverflow.com/questions/9241314/microsoft-likes-the-false-value-better

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!