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.

116 CHAPTER 4 Say<strong>in</strong>g noth<strong>in</strong>g with nullable types4.2.1 Introduc<strong>in</strong>g NullableAs you can tell by its name, Nullable is a generic type. The type parameter T has thevalue type constra<strong>in</strong>t on it. As I mentioned <strong>in</strong> section 3.3.1, this also means you can’tuse another nullable type as the argument—so Nullable is forbidden,for <strong>in</strong>stance, even though Nullable is a value type <strong>in</strong> every other way. The typeof T for any particular nullable type is called the underly<strong>in</strong>g type of that nullable type. Forexample, the underly<strong>in</strong>g type of Nullable is <strong>in</strong>t.The most important parts of Nullable are its properties, HasValue andValue. They do the obvious th<strong>in</strong>g: Value represents the non-nullable value (the“real” one, if you will) when there is one, and throws an InvalidOperation-Exception if (conceptually) there is no real value. HasValue is simply a Booleanproperty <strong>in</strong>dicat<strong>in</strong>g whether there’s a real value or whether the <strong>in</strong>stance should beregarded as null. For now, I’ll talk about an “<strong>in</strong>stance with a value” and an “<strong>in</strong>stancewithout a value,” which mean <strong>in</strong>stances where the HasValue property returns true orfalse, respectively.Now that we know what we want the properties to achieve, let’s see how to createan <strong>in</strong>stance of the type. Nullable has two constructors: the default one (creat<strong>in</strong>gan <strong>in</strong>stance without a value) and one tak<strong>in</strong>g an <strong>in</strong>stance of T as the value. Once an<strong>in</strong>stance has been constructed, it is immutable.NOTEValue types and mutability—A type is said to be immutable if it is designed sothat an <strong>in</strong>stance can’t be changed after it’s been constructed. Immutabletypes often make life easier when it comes to topics such as multithread<strong>in</strong>g,where it helps to know that nobody can be chang<strong>in</strong>g values <strong>in</strong> onethread while you’re read<strong>in</strong>g them <strong>in</strong> a different one. However, immutabilityis also important for value types. As a general rule, value types shouldalmost always be immutable. If you need a way of bas<strong>in</strong>g one value onanother, follow the lead of DateTime and TimeSpan—provide methodsthat return a new value rather than modify<strong>in</strong>g an exist<strong>in</strong>g one. That way,you avoid situations where you th<strong>in</strong>k you’re chang<strong>in</strong>g a variable but actuallyyou’re chang<strong>in</strong>g the value returned by a property or method, which is justa copy of the variable’s value. The compiler is usually smart enough towarn you about this, but it’s worth try<strong>in</strong>g to avoid the situation <strong>in</strong> the firstplace. Very few value types <strong>in</strong> the framework are mutable, fortunately.Nullable <strong>in</strong>troduces a s<strong>in</strong>gle new method, GetValueOrDefault, which has twooverloads. Both return the value of the <strong>in</strong>stance if there is one, or a default value otherwise.One overload doesn’t have any parameters (<strong>in</strong> which case the generic defaultvalue of the underly<strong>in</strong>g type is used), and the other allows you to specify the defaultvalue to return if necessary.The other methods implemented by Nullable all override exist<strong>in</strong>g methods:GetHashCode, ToStr<strong>in</strong>g, and Equals. GetHashCode returns 0 if the <strong>in</strong>stance doesn’thave a value, or the result of call<strong>in</strong>g GetHashCode on the value if there is one.ToStr<strong>in</strong>g returns an empty str<strong>in</strong>g if there isn’t a value, or the result of call<strong>in</strong>gLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!