如何确定可空布尔值的逻辑运算符的结果?

Moa*_*ini 0 .net c# boolean operators

例如,在C#中,当我比较两个可空的布尔值(bool?)时,我得到以下结果:

true & null = null
false & null = false
true | null = true
false | null = null
Run Code Online (Sandbox Code Playgroud)

问题是我无法理解这些结果是如何产生的,当我们中的一个为空时,我可以使用什么规则来确定两个布尔值上的逻辑运算符的结果?

Jon*_*eet 11

这个想法是"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
Run Code Online (Sandbox Code Playgroud)