Prev | Current Page 257 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"

Boolean logic is simple like that??”but what should happen when you??™ve
got a tristate logical type? Well, bool? is just such a type??”the value can be true,
false, or null. That means that our truth tables now have to have nine rows for our
binary operators, as there are nine combinations. The specification only highlights
the logical AND and inclusive OR operators (& and |, respectively) because the other
operators??”unary logical negation (!) and exclusive OR (^)??”follow the same rules as
other lifted operators. There are no conditional logical operators (the short-circuiting
&& and || operators) defined for bool?, which makes life simpler.
For the sake of completeness, table 4.2 gives the truth tables for all four valid bool?
operators.
Table 4.2 Truth table for the logical operators AND, inclusive OR, exclusive OR, and logical
negation, applied to the bool? type
x y x & y x | y x ^ y !x
true true true true false false
true false false true true false
true null null true null false
false true false true true true
false false false false false true
false null false null null true
128 CHAPTER 4 Saying nothing with nullable types
For those who find reasoning about rules easier to understand than looking up values
in tables, the idea is that a null bool? value is in some senses a ???maybe.??? If you imagine
that each null entry in the input side of the table is a variable instead, then you will
always get a null value on the output side of the table if the result depends on the
value of that variable.


Pages:
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269