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.

114 CHAPTER 4 Say<strong>in</strong>g noth<strong>in</strong>g with nullable typesyou often care about hav<strong>in</strong>g the whole range of possible bit patterns available as realvalues, whereas with reference types we’re happy enough to lose one potential value<strong>in</strong> order to ga<strong>in</strong> the benefits of hav<strong>in</strong>g a null value.That’s the usual situation—now why would you want to be able to represent nullfor a value type anyway? The most common immediate reason is simply because databasestypically support NULL as a value for every type (unless you specifically make thefield non-nullable), so you can have nullable character data, nullable <strong>in</strong>tegers, nullableBooleans—the whole works. When you fetch data from a database, it’s generallynot a good idea to lose <strong>in</strong>formation, so you want to be able to represent the nullity ofwhatever you read, somehow.That just moves the question one step further on, though. Why do databasesallow null values for dates, <strong>in</strong>tegers and the like? Null values are typically used forunknown or miss<strong>in</strong>g values such as the dispatch date <strong>in</strong> our earlier e-commerceexample. Nullity represents an absence of def<strong>in</strong>ite <strong>in</strong>formation, which can be important<strong>in</strong> many situations.That br<strong>in</strong>gs us to options for represent<strong>in</strong>g null values <strong>in</strong> <strong>C#</strong> 1.4.1.2 Patterns for represent<strong>in</strong>g null values <strong>in</strong> <strong>C#</strong> 1There are three basic patterns commonly used to get around the lack of nullablevalue types <strong>in</strong> <strong>C#</strong> 1. Each of them has its pros and cons—mostly cons—and all of themare fairly unsatisfy<strong>in</strong>g. However, it’s worth know<strong>in</strong>g them, partly to more fully appreciatethe benefits of the <strong>in</strong>tegrated solution <strong>in</strong> <strong>C#</strong> 2.PATTERN 1: THE MAGIC VALUEThe first pattern tends to be used as the solution for DateTime, because few peopleexpect their databases to actually conta<strong>in</strong> dates <strong>in</strong> 1AD. In other words, it goes aga<strong>in</strong>st thereason<strong>in</strong>g I gave earlier, expect<strong>in</strong>g every possible value to be available. So, we sacrificeone value (typically DateTime.M<strong>in</strong>Value) to mean a null value. The semantic mean<strong>in</strong>g ofthat will vary from application to application—it may mean that the user hasn’t enteredthe value <strong>in</strong>to a form yet, or that it’s <strong>in</strong>appropriate for that record, for example.The good news is that us<strong>in</strong>g a magic value doesn’t waste any memory or need anynew types. However, it does rely on you pick<strong>in</strong>g an appropriate value that will never beone you actually want to use for real data. Also, it’s basically <strong>in</strong>elegant. It just doesn’tfeel right. If you ever f<strong>in</strong>d yourself need<strong>in</strong>g to go down this path, you should at leasthave a constant (or static read-only value for types that can’t be expressed as constants)represent<strong>in</strong>g the magic value—comparisons with DateTime.M<strong>in</strong>Value everywhere,for <strong>in</strong>stance, don’t express the mean<strong>in</strong>g of the magic value.ADO.NET has a variation on this pattern where the same magic value—DBNull.Value—is used for all null values, of whatever type. In this case, an extra valueand <strong>in</strong>deed an extra type have been <strong>in</strong>troduced to <strong>in</strong>dicate when a database hasreturned null. However, it’s only applicable where compile-time type safety isn’timportant (<strong>in</strong> other words when you’re happy to use object and cast after test<strong>in</strong>g fornullity), and aga<strong>in</strong> it doesn’t feel quite right. In fact, it’s a mixture of the “magic value”pattern and the “reference type wrapper” pattern, which we’ll look at next.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!