13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

122 CHAPTER 4 Say<strong>in</strong>g noth<strong>in</strong>g with nullable typesGiven that the <strong>C#</strong> 2 specification def<strong>in</strong>es the null value, it would be pretty odd if wecouldn’t use the null literal we’ve already got <strong>in</strong> the language <strong>in</strong> order to represent it.Fortunately we can, as our next section will show.4.3.2 Assign<strong>in</strong>g and compar<strong>in</strong>g with nullA very concise author could cover this whole section <strong>in</strong> a s<strong>in</strong>gle sentence: “The <strong>C#</strong>compiler allows the use of null to represent the null value of a nullable type <strong>in</strong> bothcomparisons and assignments.” I prefer to show you what it means <strong>in</strong> real code, as wellas th<strong>in</strong>k about why the language has been given this feature.You may have felt a bit uncomfortable every time we’ve used the default constructorof Nullable. It achieves the desired behavior, but it doesn’t express the reasonwe want to do it—it doesn’t leave the right impression with the reader. We want togive the same sort of feel<strong>in</strong>g that us<strong>in</strong>g null does with reference types. If it seems oddto you that I’ve talked about feel<strong>in</strong>gs <strong>in</strong> both this section and the last one, just th<strong>in</strong>kabout who writes code, and who reads it. Sure, the compiler has to understand thecode, and it couldn’t care less about the subtle nuances of style—but very few piecesof code used <strong>in</strong> production systems are written and then never read aga<strong>in</strong>. Anyth<strong>in</strong>gyou can do to get the reader <strong>in</strong>to the mental process you were go<strong>in</strong>g through whenyou orig<strong>in</strong>ally wrote the code is good—and us<strong>in</strong>g the familiar null literal helps toachieve that.With that <strong>in</strong> m<strong>in</strong>d, we’re go<strong>in</strong>g to change the example we’re us<strong>in</strong>g from one thatjust shows syntax and behavior to one that gives an impression of how nullable typesmight be used. We’ll consider model<strong>in</strong>g a Person class where you need to know thename, date of birth, and date of death of a person. We’ll only keep track of peoplewho have def<strong>in</strong>itely been born, but some of those people may still be alive—<strong>in</strong> whichcase our date of death is represented by null. List<strong>in</strong>g 4.4 shows some of the possiblecode. Although a real class would clearly have more operations available, we’re justlook<strong>in</strong>g at the calculation of age for this example.List<strong>in</strong>g 4.4Part of a Person class <strong>in</strong>clud<strong>in</strong>g calculation of ageclass Person{DateTime birth;DateTime? death;str<strong>in</strong>g name;public TimeSpan Age{get{if (death==null){BChecksHasValuereturn DateTime.Now-birth;}else{return death.Value-birth;CUnwraps forcalculationLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!