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.

System.Nullable and System.Nullable115PATTERN 2: A REFERENCE TYPE WRAPPERThe second solution can take two forms. The simpler one is to just use object as thevariable type, box<strong>in</strong>g and unbox<strong>in</strong>g values as necessary. The more complex (andrather more appeal<strong>in</strong>g) form is to have a reference type for each value type you need<strong>in</strong> a nullable form, conta<strong>in</strong><strong>in</strong>g a s<strong>in</strong>gle <strong>in</strong>stance variable of that value type, and withimplicit conversion operators to and from the value type. With generics, you could dothis <strong>in</strong> one generic type—but if you’re us<strong>in</strong>g <strong>C#</strong> 2 anyway, you might as well use thenullable types described <strong>in</strong> this chapter <strong>in</strong>stead. If you’re stuck <strong>in</strong> <strong>C#</strong> 1, you have tocreate extra source code for each type you wish to wrap. This isn’t hard to put <strong>in</strong> theform of a template for automatic code generation, but it’s still a burden that is bestavoided if possible.Both of these forms have the problem that while they allow you to use nulldirectly, they do require objects to be created on the heap, which can lead to garbagecollection pressure if you need to use this approach very frequently, and adds memoryuse due to the overheads associated with objects. For the more complex solution, youcould make the reference type mutable, which may reduce the number of <strong>in</strong>stancesyou need to create but could also make for some very un<strong>in</strong>tuitive code.PATTERN 3: AN EXTRA BOOLEAN FLAGThe f<strong>in</strong>al pattern revolves around hav<strong>in</strong>g a normal value type value available, andanother value—a Boolean flag—<strong>in</strong>dicat<strong>in</strong>g whether the value is “real” or whether itshould be disregarded. Aga<strong>in</strong>, there are two ways of implement<strong>in</strong>g this solution.Either you could ma<strong>in</strong>ta<strong>in</strong> two separate variables <strong>in</strong> the code that uses the value, oryou could encapsulate the “value plus flag” <strong>in</strong>to another value type.This latter solution is quite similar to the more complicated reference type ideadescribed earlier, except that you avoid the garbage-collection issue by us<strong>in</strong>g a valuetype, and <strong>in</strong>dicate nullity with<strong>in</strong> the encapsulated value rather than by virtue of a nullreference. The downside of hav<strong>in</strong>g to create a new one of these types for every valuetype you wish to handle is the same, however. Also, if the value is ever boxed for somereason, it will be boxed <strong>in</strong> the normal way whether it’s considered to be null or not.The last pattern (<strong>in</strong> the more encapsulated form) is effectively how nullable typeswork <strong>in</strong> <strong>C#</strong> 2. We’ll see that when the new features of the framework, CLR, and languageare all comb<strong>in</strong>ed, the solution is significantly neater than anyth<strong>in</strong>g that was possible <strong>in</strong><strong>C#</strong> 1. Our next section deals with just the support provided by the framework and theCLR: if <strong>C#</strong> 2 only supported generics, the whole of section 4.2 would still be relevant andthe feature would still work and be useful. However, <strong>C#</strong> 2 provides extra syntactic sugarto make it even better—that’s the subject of section 4.3.4.2 System.Nullable and System.NullableThe core structure at the heart of nullable types is System.Nullable. In addition,the System.Nullable static class provides utility methods that occasionally make nullabletypes easier to work with. (From now on I’ll leave out the namespace, to make lifesimpler.) We’ll look at both of these types <strong>in</strong> turn, and for this section I’ll avoid any extrafeatures provided by the language, so you’ll be able to understand what’s go<strong>in</strong>g on <strong>in</strong>the IL code when we do look at the <strong>C#</strong> 2 syntactic sugar.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!