可空 bool 逻辑 "AND" 运算符对 true 和 false 的行为不同
Nullable bool logical "AND" operator behaves differently with true and false
使用 bool?
变量,有人可以解释为什么 true & null
导致 null
而 false & null
导致 false
吗?
void Main()
{
bool? a = null;
bool? b = true;
bool? c = a & b;
Console.WriteLine($"{b} & NULL => " + c);
b = false;
c = a & b;
Console.WriteLine($"{b} & NULL => " + c);
}
输出:
True & NULL =>
False & NULL => False
我也会对副本感到高兴,因为我还没有找到它。
在这个条件下(a & b),编译器只是当找到 1 false , returns false, else compare a and b, in his condition cant return real result, then return没什么!
使用可空类型意味着您的布尔值实际上可以有 3 个值,true
、false
和 null
。这意味着你必须根据 three-valued logic.
(针对 sql 问题)涵盖了使用三值逻辑时 AND
和 OR
的结果。
答案是布尔值? type 被设计为匹配 SQL 布尔值,如此处所述
Using nullable types (C# Programming Guide)
并在下面引用
The bool? type
The bool? nullable type can contain three different values: true,
false, and null. The bool? type is like the Boolean variable type that
is used in SQL. To ensure that the results produced by the & and |
operators are consistent with the three-valued Boolean type in SQL,
the following predefined operators are provided: •bool? operator
&(bool? x, bool? y) •bool? operator |(bool? x, bool? y)
The semantics of these operators is defined by the following table:
x y x&y x|y
true true true true
true false false true
true null null true
false true false true
false false false false
false null false null
null true null true
null false false null
null null null null
Note that these two operators don't follow the rules described in the
Operators section: the result of an operator evaluation can be
non-null even if one of the operands is null.
在这种情况下,null
的目的是识别未知值。在三值逻辑中,unknown
值的目的是表明我们对谓词的真值或假值一无所知。
false AND unknown
returns false
因为知道第二个运算符是什么并不重要。这是由于 and 运算符的性质要求两个操作数都是 true
到 return true
。我们知道第一个操作数是false
,其余的无关紧要,结果是false
.
另一方面,true AND unknown
returnsunknown
,因为第一个操作数是true
,所以整个操作的结果取决于第二个操作数。第二个操作数是 unknown
,因此结果是 unknown
.
C# 表示 unknown
和 null
.
使用 bool?
变量,有人可以解释为什么 true & null
导致 null
而 false & null
导致 false
吗?
void Main()
{
bool? a = null;
bool? b = true;
bool? c = a & b;
Console.WriteLine($"{b} & NULL => " + c);
b = false;
c = a & b;
Console.WriteLine($"{b} & NULL => " + c);
}
输出:
True & NULL =>
False & NULL => False
我也会对副本感到高兴,因为我还没有找到它。
在这个条件下(a & b),编译器只是当找到 1 false , returns false, else compare a and b, in his condition cant return real result, then return没什么!
使用可空类型意味着您的布尔值实际上可以有 3 个值,true
、false
和 null
。这意味着你必须根据 three-valued logic.
AND
和 OR
的结果。
答案是布尔值? type 被设计为匹配 SQL 布尔值,如此处所述 Using nullable types (C# Programming Guide) 并在下面引用
The bool? type
The bool? nullable type can contain three different values: true, false, and null. The bool? type is like the Boolean variable type that is used in SQL. To ensure that the results produced by the & and | operators are consistent with the three-valued Boolean type in SQL, the following predefined operators are provided: •bool? operator &(bool? x, bool? y) •bool? operator |(bool? x, bool? y)
The semantics of these operators is defined by the following table:
x y x&y x|y
true true true true
true false false true
true null null true
false true false true
false false false false
false null false null
null true null true
null false false null
null null null nullNote that these two operators don't follow the rules described in the Operators section: the result of an operator evaluation can be non-null even if one of the operands is null.
在这种情况下,null
的目的是识别未知值。在三值逻辑中,unknown
值的目的是表明我们对谓词的真值或假值一无所知。
false AND unknown
returns false
因为知道第二个运算符是什么并不重要。这是由于 and 运算符的性质要求两个操作数都是 true
到 return true
。我们知道第一个操作数是false
,其余的无关紧要,结果是false
.
另一方面,true AND unknown
returnsunknown
,因为第一个操作数是true
,所以整个操作的结果取决于第二个操作数。第二个操作数是 unknown
,因此结果是 unknown
.
C# 表示 unknown
和 null
.