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.

128 CHAPTER 4 Say<strong>in</strong>g noth<strong>in</strong>g with nullable typesTable 4.2Truth table for the logical operators AND, <strong>in</strong>clusive OR, exclusive OR, and logicalnegation, applied to the bool? type (cont<strong>in</strong>ued)x y x & y x | y x ^ y !xnull true null true null nullnull false false null null nullnull null null null null nullFor those who f<strong>in</strong>d reason<strong>in</strong>g about rules easier to understand than look<strong>in</strong>g up values<strong>in</strong> tables, the idea is that a null bool? value is <strong>in</strong> some senses a “maybe.” If you imag<strong>in</strong>ethat each null entry <strong>in</strong> the <strong>in</strong>put side of the table is a variable <strong>in</strong>stead, then you willalways get a null value on the output side of the table if the result depends on thevalue of that variable. For <strong>in</strong>stance, look<strong>in</strong>g at the third l<strong>in</strong>e of the table, the expressiontrue & y will only be true if y is true, but the expression true | y will always betrue whatever the value of y is, so the nullable results are null and true, respectively.When consider<strong>in</strong>g the lifted operators and particularly how nullable logic works, thelanguage designers had two slightly contradictory sets of exist<strong>in</strong>g behavior—<strong>C#</strong> 1 nullreferences and SQL NULL values. In many cases these don’t conflict at all—<strong>C#</strong> 1 had noconcept of apply<strong>in</strong>g logical operators to null references, so there was no problem <strong>in</strong>us<strong>in</strong>g the SQL-like results given earlier. The def<strong>in</strong>itions we’ve seen may surprise someSQL developers, however, when it comes to comparisons. In standard SQL, the result ofcompar<strong>in</strong>g two values (<strong>in</strong> terms of equality or greater than/less than) is always unknownif either value is NULL. The result <strong>in</strong> <strong>C#</strong> 2 is never null, and <strong>in</strong> particular two null valuesare considered to be equal to each other.NOTE Rem<strong>in</strong>der: this is <strong>C#</strong> specific! It’s worth remember<strong>in</strong>g that the lifted operatorsand conversions, along with the bool? logic described <strong>in</strong> this section,are all provided by the <strong>C#</strong> compiler and not by the CLR or the frameworkitself. If you use ildasm on code that evaluates any of these nullable operators,you’ll f<strong>in</strong>d that the compiler has created all the appropriate IL totest for null values and deal with them accord<strong>in</strong>gly. This means that differentlanguages can behave differently on these matters—def<strong>in</strong>itelysometh<strong>in</strong>g to look out for if you need to port code between different.NET-based languages.We now certa<strong>in</strong>ly know enough to use nullable types and predict how they’ll behave,but <strong>C#</strong> 2 has a sort of “bonus track” when it comes to syntax enhancements: the nullcoalesc<strong>in</strong>g operator.4.3.5 The null coalesc<strong>in</strong>g operatorAside from the ? modifier, all of the rest of the <strong>C#</strong> compiler’s tricks so far to do withnullable types have worked with the exist<strong>in</strong>g syntax. However, <strong>C#</strong> 2 <strong>in</strong>troduces a newoperator that can occasionally make code shorter and sweeter. It’s called the null coalesc<strong>in</strong>goperator and appears <strong>in</strong> code as ?? between its two operands. It’s a bit like theconditional operator but specially tweaked for nulls.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!