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.

120 CHAPTER 4 Say<strong>in</strong>g noth<strong>in</strong>g with nullable typesconta<strong>in</strong>s static methods, and you can’t create an <strong>in</strong>stance of it. 2 In fact, everyth<strong>in</strong>g itdoes could have been done equally well by other types, and if Microsoft had seenwhere they were go<strong>in</strong>g right from the beg<strong>in</strong>n<strong>in</strong>g, it might not have even existed—which would have saved a little confusion over what the two types are there for, asidefrom anyth<strong>in</strong>g else. However, this accident of history has three methods to its name,and they’re still useful.The first two are comparison methods:public static <strong>in</strong>t Compare(Nullable n1, Nullable n2)public static bool Equals(Nullable n1, Nullable n2)Compare uses Comparer.Default to compare the two underly<strong>in</strong>g values (if theyexist), and Equals uses EqualityComparer.Default. In the face of <strong>in</strong>stances withno values, the values returned from each method comply with the .NET conventionsof nulls compar<strong>in</strong>g equal to each other and less than anyth<strong>in</strong>g else.Both of these methods could quite happily be part of Nullable as static butnongeneric methods. The one small advantage of hav<strong>in</strong>g them as generic methods <strong>in</strong>a nongeneric type is that generic type <strong>in</strong>ference can be applied, so you’ll rarely needto explicitly specify the type parameter.The f<strong>in</strong>al method of System.Nullable isn’t generic—<strong>in</strong>deed, it absolutely couldn’tbe. Its signature is as follows:public static Type GetUnderly<strong>in</strong>gType (Type nullableType)If the parameter is a nullable type, the method returns its underly<strong>in</strong>g type; otherwiseit returns null. The reason this couldn’t be a generic method is that if you knew theunderly<strong>in</strong>g type to start with, you wouldn’t have to call it!We’ve now seen what the framework and the CLR provide to support nullabletypes—but <strong>C#</strong> 2 adds language features to make life a lot more pleasant.4.3 <strong>C#</strong> 2’s syntactic sugar for nullable typesThe examples so far have shown nullable types do<strong>in</strong>g their job, but they’ve not beenparticularly pretty to look at. Admittedly it makes it obvious that you are us<strong>in</strong>g nullabletypes when you have to type Nullable around the name of the type you’re really<strong>in</strong>terested <strong>in</strong>, but it makes the nullability more prom<strong>in</strong>ent than the name of the typeitself, which is surely not a good idea.In addition, the very name “nullable” suggests that we should be able to assignnull to a variable of a nullable type, and we haven’t seen that—we’ve always used thedefault constructor of the type. In this section we’ll see how <strong>C#</strong> 2 deals with theseissues and others.Before we get <strong>in</strong>to the details of what <strong>C#</strong> 2 provides as a language, there’s one def<strong>in</strong>itionI can f<strong>in</strong>ally <strong>in</strong>troduce. The null value of a nullable type is the value whereHasValue returns false—or an “<strong>in</strong>stance without a value,” as I’ve referred to it <strong>in</strong> section4.2. I didn’t use it before because it’s specific to <strong>C#</strong>. The CLI specification2You’ll learn more about static classes <strong>in</strong> chapter 7.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!