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.

Say<strong>in</strong>g noth<strong>in</strong>gwith nullable typesThis chapter covers■■Motivation for null valuesFramework and runtime support■ Language support <strong>in</strong> <strong>C#</strong> 2■Patterns us<strong>in</strong>g nullable typesNullity is a concept that has provoked a certa<strong>in</strong> amount of debate over the years. Isa null reference a value, or the absence of a value? Is “noth<strong>in</strong>g” a “someth<strong>in</strong>g”? Inthis chapter, I’ll try to stay more practical than philosophical. First we’ll look at whythere’s a problem <strong>in</strong> the first place—why you can’t set a value type variable to null<strong>in</strong> <strong>C#</strong> 1 and what the traditional alternatives have been. After that I’ll <strong>in</strong>troduce youto our knight <strong>in</strong> sh<strong>in</strong><strong>in</strong>g armor—System.Nullable—before we see how <strong>C#</strong> 2makes work<strong>in</strong>g with nullable types a bit simpler and more compact. Like generics,nullable types sometimes have some uses beyond what you might expect, and we’lllook at a few examples of these at the end of the chapter.So, when is a value not a value? Let’s f<strong>in</strong>d out.112Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!