13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>C#</strong> 2’s syntactic sugar for nullable types127Now we’re able to answer the question at the end of the previous section—why weused death.Value-birth <strong>in</strong> list<strong>in</strong>g 4.4 <strong>in</strong>stead of just death-birth. Apply<strong>in</strong>g the previousrules, we could have used the latter expression, but the result would have been aTimeSpan? <strong>in</strong>stead of a TimeSpan. This would have left us with the options of cast<strong>in</strong>g theresult to TimeSpan, us<strong>in</strong>g its Value property, or chang<strong>in</strong>g the Age property to return aTimeSpan?—which just pushes the issue onto the caller. It’s still a bit ugly, but we’ll seea nicer implementation of the Age property <strong>in</strong> section 4.3.5.In the list of restrictions regard<strong>in</strong>g operator lift<strong>in</strong>g, I mentioned that bool? worksslightly differently than the other types. Our next section expla<strong>in</strong>s this and pulls backthe lens to see the bigger picture of why all these operators work the way they do.4.3.4 Nullable logicI vividly remember my early electronics lessons at school. They always seemed torevolve around either work<strong>in</strong>g out the voltage across different parts of a circuit us<strong>in</strong>gthe V=IR formula, or apply<strong>in</strong>g truth tables—the reference charts for expla<strong>in</strong><strong>in</strong>g the differencebetween NAND gates and NOR gates and so on. The idea is simple—a truthtable maps out every possible comb<strong>in</strong>ation of <strong>in</strong>puts <strong>in</strong>to whatever piece of logicyou’re <strong>in</strong>terested <strong>in</strong> and tells you the output.The truth tables we drew for simple, two-<strong>in</strong>put logic gates always had four rows—each of the two <strong>in</strong>puts had two possible values, which means there were four possiblecomb<strong>in</strong>ations. Boolean logic is simple like that—but what should happen when you’vegot 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 n<strong>in</strong>e rows for ourb<strong>in</strong>ary operators, as there are n<strong>in</strong>e comb<strong>in</strong>ations. The specification only highlightsthe logical AND and <strong>in</strong>clusive OR operators (& and |, respectively) because the otheroperators—unary logical negation (!) and exclusive OR (^)—follow the same rules asother lifted operators. There are no conditional logical operators (the short-circuit<strong>in</strong>g&& and || operators) def<strong>in</strong>ed 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.2Truth table for the logical operators AND, <strong>in</strong>clusive OR, exclusive OR, and logicalnegation, applied to the bool? typex y x & y x | y x ^ y !xtrue true true true false falsetrue false false true true falsetrue null null true null falsefalse true false true true truefalse false false false false truefalse null false null null trueLicensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!