Prev | Current Page 254 | Next

Jon Skeet

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

The equality and relational operators
keep their non-nullable Boolean return types. For equality, two null values are considered
equal, and a null value and any non-null value are considered different, which is
consistent with the behavior we saw in section 4.2.3. The relational operators always
return 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 invoked in the obvious way.
All these rules sound more complicated than they really are??”for the most part,
everything works as you probably expect it to. It??™s easiest to see what happens with a
few examples, and as int has so many predefined operators (and integers can be so
easily expressed), it??™s the natural demonstration type. Table 4.1 shows a number of
expressions, the lifted operator signature, and the result. It is assumed that there are
variables four, five, and nullInt, each with type int? and with the obvious values.
Possibly the most surprising line of the table is the bottom one??”that a null value
isn??™t deemed ???less than or equal to??? another null value, even though they are deemed
4 The equality and relational operators are, of course, binary operators themselves, but we??™ll see that they
behave slightly differently to the others, hence their separation here.
126 CHAPTER 4 Saying nothing with nullable types
to be equal to each other (as per the fifth row)! Very odd??”but unlikely to cause problems
in real life, in my experience.


Pages:
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266