13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>C#</strong> 2’s syntactic sugar for nullable types125So far, so relatively simple. Now let’s consider the operators, where th<strong>in</strong>gs areslightly more tricky.OPERATORS INVOLVING NULLABLE TYPES<strong>C#</strong> allows the follow<strong>in</strong>g operators to be overloaded:■ Unary: + ++ - -- ! ~ true false■ B<strong>in</strong>ary: + - * / % & | ^ >■ Equality: 4 == !=■ Relational: < > =When these operators are overloaded for a non-nullable value type T, the nullable typeT? has the same operators, with slightly different operand and result types. These arecalled lifted operators whether they’re predef<strong>in</strong>ed operators like addition on numerictypes, or user-def<strong>in</strong>ed operators like add<strong>in</strong>g a TimeSpan to a DateTime. There are a fewrestrictions as to when they apply:■ The true and false operators are never lifted. They’re <strong>in</strong>credibly rare <strong>in</strong> thefirst place, though, so it’s no great loss.■ Only operators with non-nullable value types for the operands are lifted.■ For the unary and b<strong>in</strong>ary operators (other than equality and relational operators),the return type has to be a non-nullable value type.■ For the equality and relational operators, the return type has to be bool.■ The & and | operators on bool? have separately def<strong>in</strong>ed behavior, which we’llsee <strong>in</strong> section 4.3.6.For all the operators, the operand types become their nullable equivalents. For theunary and b<strong>in</strong>ary operators, the return type also becomes nullable, and a null value isreturned if any of the operands is a null value. The equality and relational operatorskeep their non-nullable Boolean return types. For equality, two null values are consideredequal, and a null value and any non-null value are considered different, which isconsistent with the behavior we saw <strong>in</strong> section 4.2.3. The relational operators alwaysreturn false if either operand is a null value. When none of the operands is a null value,the operator of the non-nullable type is <strong>in</strong>voked <strong>in</strong> the obvious way.All these rules sound more complicated than they really are—for the most part,everyth<strong>in</strong>g works as you probably expect it to. It’s easiest to see what happens with afew examples, and as <strong>in</strong>t has so many predef<strong>in</strong>ed operators (and <strong>in</strong>tegers can be soeasily expressed), it’s the natural demonstration type. Table 4.1 shows a number ofexpressions, the lifted operator signature, and the result. It is assumed that there arevariables four, five, and nullInt, each with type <strong>in</strong>t? and with the obvious values.Possibly the most surpris<strong>in</strong>g l<strong>in</strong>e of the table is the bottom one—that a null valueisn’t deemed “less than or equal to” another null value, even though they are deemed4The equality and relational operators are, of course, b<strong>in</strong>ary operators themselves, but we’ll see that theybehave slightly differently to the others, hence their separation here.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!