Prev | Current Page 244 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"

Default to compare the two underlying values (if they
exist), and Equals uses EqualityComparer.Default. In the face of instances with
no values, the values returned from each method comply with the .NET conventions
of nulls comparing equal to each other and less than anything else.
Both of these methods could quite happily be part of Nullable as static but
nongeneric methods. The one small advantage of having them as generic methods in
a nongeneric type is that generic type inference can be applied, so you??™ll rarely need
to explicitly specify the type parameter.
The final method of System.Nullable isn??™t generic??”indeed, it absolutely couldn??™t
be. Its signature is as follows:
public static Type GetUnderlyingType (Type nullableType)
If the parameter is a nullable type, the method returns its underlying type; otherwise
it returns null. The reason this couldn??™t be a generic method is that if you knew the
underlying type to start with, you wouldn??™t have to call it!
We??™ve now seen what the framework and the CLR provide to support nullable
types??”but C# 2 adds language features to make life a lot more pleasant.
4.3 C# 2??™s syntactic sugar for nullable types
The examples so far have shown nullable types doing their job, but they??™ve not been
particularly pretty to look at. Admittedly it makes it obvious that you are using nullable
types when you have to type Nullable<> around the name of the type you??™re really
interested in, but it makes the nullability more prominent than the name of the type
itself, which is surely not a good idea.


Pages:
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256