The nullable boolean type bool? can represent three values, true, false, and null, and is conceptually similar to the three-valued type used for boolean expressions in SQL. To ensure that the results produced by the & and | operators for bool? operands are consistent with SQL’s three-valued logic, the following predefined operators are provided:
bool? operator &(bool? x, bool? y);
bool? operator |(bool? x, bool? y);
The following table lists the results produced by these operators for all combinations of the values true, false, and null.
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
|
Conditional logical operators
The && and || operators are called the conditional logical operators. They are also called the “short-circuiting” logical operators.
conditional-and-expression:
inclusive-or-expression
conditional-and-expression && inclusive-or-expression
conditional-or-expression:
conditional-and-expression
conditional-or-expression || conditional-and-expression
The && and || operators are conditional versions of the & and | operators:
The operation x && y corresponds to the operation x & y, except that y is evaluated only if x is not false.
The operation x || y corresponds to the operation x | y, except that y is evaluated only if x is not true.
If an operand of a conditional logical operator has the compile-time type dynamic, then the expression is dynamically bound (§7.2.2). In this case the compile-time type of the expression is dynamic, and the resolution described below will take place at run-time using the run-time type of those operands that have the compile-time type dynamic.
An operation of the form x && y or x || y is processed by applying overload resolution (§7.3.4) as if the operation was written x & y or x | y. Then,
If overload resolution fails to find a single best operator, or if overload resolution selects one of the predefined integer logical operators, a binding-time error occurs.
Otherwise, if the selected operator is one of the predefined boolean logical operators (§7.11.3) or nullable boolean logical operators (§7.11.4), the operation is processed as described in §7.12.1.
Otherwise, the selected operator is a user-defined operator, and the operation is processed as described in §7.12.2.
It is not possible to directly overload the conditional logical operators. However, because the conditional logical operators are evaluated in terms of the regular logical operators, overloads of the regular logical operators are, with certain restrictions, also considered overloads of the conditional logical operators. This is described further in §7.12.2.
Do'stlaringiz bilan baham: |