【发布时间】:2010-11-25 20:06:42
【问题描述】:
例如,在 C# 中,当我比较两个可为空的布尔值 (bool?) 时,我得到以下结果:
true & null = null
false & null = false
true | null = true
false | null = null
问题是我无法理解这些结果是如何产生的,当其中一个为空时,我可以使用什么规则来确定逻辑运算符对两个布尔值的结果?
【问题讨论】:
例如,在 C# 中,当我比较两个可为空的布尔值 (bool?) 时,我得到以下结果:
true & null = null
false & null = false
true | null = true
false | null = null
问题是我无法理解这些结果是如何产生的,当其中一个为空时,我可以使用什么规则来确定逻辑运算符对两个布尔值的结果?
【问题讨论】:
这里的想法是“null”意味着“未知”,或者“信息不足”。因此,如果答案取决于未知值,则答案本身是未知的。如果无论未知值是什么(例如true | null)答案都相同,那么您仍然可以。
这样想:
y = true & x; // the result will be the same as the value of x (it depends on x)
y = true | x; // the result will be true whatever x is
【讨论】:
it has to be x 而不是 it has to be true,即 y 将是 x 将是什么。