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.

124 CHAPTER 4 Say<strong>in</strong>g noth<strong>in</strong>g with nullable types4.3.3 Nullable conversions and operatorsWe’ve seen that we can compare <strong>in</strong>stances of nullable types with null, but there areother comparisons that can be made and other operators that can be used <strong>in</strong> somecases. Likewise we’ve seen wrapp<strong>in</strong>g and unwrapp<strong>in</strong>g, but other conversions can beused with some types. This section expla<strong>in</strong>s what’s available. I’m afraid it’s pretty muchimpossible to make this k<strong>in</strong>d of topic genu<strong>in</strong>ely excit<strong>in</strong>g, but carefully designed featureslike these are what make <strong>C#</strong> a pleasant language to work with <strong>in</strong> the long run.Don’t worry if not all of it s<strong>in</strong>ks <strong>in</strong> the first time: just remember that the details are hereif you need to reference them <strong>in</strong> the middle of a cod<strong>in</strong>g session.The “executive summary” is that if there is an operator or conversion available ona non-nullable value type, and that operator or conversion only <strong>in</strong>volves other nonnullablevalue types, then the nullable value type also has the same operator or conversionavailable, usually convert<strong>in</strong>g the non-nullable value types <strong>in</strong>to their nullableequivalents. To give a more concrete example, there’s an implicit conversion from <strong>in</strong>tto long, and that means there’s also an implicit conversion from <strong>in</strong>t? to long? thatbehaves <strong>in</strong> the obvious manner.Unfortunately, although that broad description gives the right general idea, theexact rules are slightly more complicated. Each one is simple, but there are quite a fewof them. It’s worth know<strong>in</strong>g about them because otherwise you may well end up star<strong>in</strong>gat a compiler error or warn<strong>in</strong>g for a while, wonder<strong>in</strong>g why it believes you’re try<strong>in</strong>gto make a conversion that you never <strong>in</strong>tended <strong>in</strong> the first place. We’ll start with theconversions, and then look at operators.CONVERSIONS INVOLVING NULLABLE TYPESFor completeness, let’s start with the conversions we already know about:■ An implicit conversion from the null literal to T?■ An implicit conversion from T to T?■An explicit conversion from T? to TNow consider the predef<strong>in</strong>ed and user-def<strong>in</strong>ed conversions available on types. For<strong>in</strong>stance, there is a predef<strong>in</strong>ed conversion from <strong>in</strong>t to long. For any conversion likethis, from one non-nullable value type (S) to another (T), the follow<strong>in</strong>g conversionsare also available:■ S? to T? (explicit or implicit depend<strong>in</strong>g on orig<strong>in</strong>al conversion)■ S to T? (explicit or implicit depend<strong>in</strong>g on orig<strong>in</strong>al conversion)■ S? to T (always explicit)To carry our example forward, this means that you can convert implicitly from <strong>in</strong>t? tolong? and from <strong>in</strong>t to long? as well as explicitly from long? to <strong>in</strong>t. The conversionsbehave <strong>in</strong> the natural way, with null values of S? convert<strong>in</strong>g to null values of T?, andnon-null values us<strong>in</strong>g the orig<strong>in</strong>al conversion. As before, the explicit conversion fromS? to T will throw an InvalidOperationException when convert<strong>in</strong>g from a null valueof S?. For user-def<strong>in</strong>ed conversions, these extra conversions <strong>in</strong>volv<strong>in</strong>g nullable typesare known as lifted conversions.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!