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.

130 CHAPTER 4 Say<strong>in</strong>g noth<strong>in</strong>g with nullable typesNote how the type of the result is DateTime rather than DateTime? because we’veused DateTime.Now as the second operand. We could shorten the whole th<strong>in</strong>g to oneexpression:return (death ?? DateTime.Now)-birth;However, this is a bit more obscure—<strong>in</strong> particular, <strong>in</strong> the two-l<strong>in</strong>e version the name ofthe lastAlive variable helps the reader to see why we’re apply<strong>in</strong>g the null coalesc<strong>in</strong>goperator. I hope you agree that the two-l<strong>in</strong>e version is simpler and more readable thaneither the orig<strong>in</strong>al version us<strong>in</strong>g the if statement or the version us<strong>in</strong>g the normal conditionaloperator from <strong>C#</strong> 1. Of course, it relies on the reader understand<strong>in</strong>g what thenull coalesc<strong>in</strong>g operator does. In my experience, this is one of the least well-knownaspects of <strong>C#</strong> 2, but it’s useful enough to make it worth try<strong>in</strong>g to enlighten yourcoworkers rather than avoid<strong>in</strong>g it.There are two further aspects that <strong>in</strong>crease the operator’s usefulness, too. First, itdoesn’t just apply to nullable types—reference types can also be used; you just can’tuse a non-nullable value type for the first operand as that would be po<strong>in</strong>tless. Also, it’sright associative, which means an expression of the form first ?? second ?? third isevaluated as first ?? (second ?? third)—and so it cont<strong>in</strong>ues for more operands.You can have any number of expressions, and they’ll be evaluated <strong>in</strong> order, stopp<strong>in</strong>gwith the first non-null result. If all of the expressions evaluate to null, the result will benull too.As a concrete example of this, suppose you have an onl<strong>in</strong>e order<strong>in</strong>g system (andwho doesn’t these days?) with the concepts of a bill<strong>in</strong>g address, contact address, andshipp<strong>in</strong>g address. The bus<strong>in</strong>ess rules declare that any user must have a bill<strong>in</strong>g address,but the contact address is optional. The shipp<strong>in</strong>g address for a particular order is alsooptional, default<strong>in</strong>g to the bill<strong>in</strong>g address. These “optional” addresses are easily representedas null references <strong>in</strong> the code. To work out who to contact <strong>in</strong> the case of aproblem with a shipment, the code <strong>in</strong> <strong>C#</strong> 1 might look someth<strong>in</strong>g like this:Address contact = user.ContactAddress;if (contact==null){contact = order.Shipp<strong>in</strong>gAddress;if (contact==null){contact = user.Bill<strong>in</strong>gAddress;}}Us<strong>in</strong>g the conditional operator <strong>in</strong> this case is even more horrible. Us<strong>in</strong>g the null coalesc<strong>in</strong>goperator, however, makes the code very straightforward:Address contact = user.ContactAddress ??order.Shipp<strong>in</strong>gAddress ??user.Bill<strong>in</strong>gAddress;If the bus<strong>in</strong>ess rules changed to use the shipp<strong>in</strong>g address by default <strong>in</strong>stead of theuser’s contact address, the change here would be extremely obvious. It wouldn’t beLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!