Prev | Current Page 232 | Next

Jon Skeet

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

When you fetch data from a database, it??™s generally
not a good idea to lose information, so you want to be able to represent the nullity of
whatever you read, somehow.
That just moves the question one step further on, though. Why do databases
allow null values for dates, integers and the like? Null values are typically used for
unknown or missing values such as the dispatch date in our earlier e-commerce
example. Nullity represents an absence of definite information, which can be important
in many situations.
That brings us to options for representing null values in C# 1.
4.1.2 Patterns for representing null values in C# 1
There are three basic patterns commonly used to get around the lack of nullable
value types in C# 1. Each of them has its pros and cons??”mostly cons??”and all of them
are fairly unsatisfying. However, it??™s worth knowing them, partly to more fully appreciate
the benefits of the integrated solution in C# 2.
PATTERN 1: THE MAGIC VALUE
The first pattern tends to be used as the solution for DateTime, because few people
expect their databases to actually contain dates in 1AD. In other words, it goes against the
reasoning I gave earlier, expecting every possible value to be available. So, we sacrifice
one value (typically DateTime.MinValue) to mean a null value. The semantic meaning of
that will vary from application to application??”it may mean that the user hasn??™t entered
the value into a form yet, or that it??™s inappropriate for that record, for example.


Pages:
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244