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.

118 CHAPTER 4 Say<strong>in</strong>g noth<strong>in</strong>g with nullable types<strong>in</strong>stance. Next, we create an <strong>in</strong>stance that doesn’t have a value, and use the same members<strong>in</strong> the same order, just omitt<strong>in</strong>g the Value property and the explicit conversion to<strong>in</strong>t s<strong>in</strong>ce these would throw exceptions. The output of list<strong>in</strong>g 4.1 is as follows:Instance with value:HasValue: TrueValue: 5Explicit conversion: 5GetValueOrDefault(): 5GetValueOrDefault(10): 5ToStr<strong>in</strong>g(): "5"GetHashCode(): 5Instance without value:HasValue: FalseGetValueOrDefault(): 0GetValueOrDefault(10): 10ToStr<strong>in</strong>g(): ""GetHashCode(): 0So far, you could probably have predicted all of the results just by look<strong>in</strong>g at the membersprovided by Nullable. When it comes to box<strong>in</strong>g and unbox<strong>in</strong>g, however,there’s special behavior to make nullable types behave how we’d really like them tobehave, rather than how they’d behave if we slavishly followed the normal box<strong>in</strong>g rules.4.2.2 Box<strong>in</strong>g and unbox<strong>in</strong>gIt’s important to remember that Nullable is a struct—a value type. This means thatif you want to convert it to a reference type (object is the most obvious example), you’llneed to box it. It is only with respect to box<strong>in</strong>g and unbox<strong>in</strong>g that the CLR itself has anyspecial behavior regard<strong>in</strong>g nullable types—the rest is “standard” generics, conversions,method calls, and so forth. In fact, the behavior was only changed shortly before therelease of .NET 2.0, as the result of community requests.An <strong>in</strong>stance of Nullable is boxed to either a null reference (if it doesn’t have avalue) or a boxed value of T (if it does). You can unbox from a boxed value either toits normal type or to the correspond<strong>in</strong>g nullable type. Unbox<strong>in</strong>g a null reference willthrow a NullReferenceException if you unbox to the normal type, but will unbox toan <strong>in</strong>stance without a value if you unbox to the appropriate nullable type. This behavioris shown <strong>in</strong> list<strong>in</strong>g 4.2.List<strong>in</strong>g 4.2Box<strong>in</strong>g and unbox<strong>in</strong>g behavior of nullable typesNullable nullable = 5;object boxed = nullable;Console.WriteL<strong>in</strong>e(boxed.GetType());Boxes a nullablewith value<strong>in</strong>t normal = (<strong>in</strong>t)boxed;Console.WriteL<strong>in</strong>e(normal);nullable = (Nullable)boxed;Console.WriteL<strong>in</strong>e(nullable);Unboxes to nonnullablevariableUnboxes tonullable variableLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!