Prev | Current Page 259 | Next

Jon Skeet

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

This means that different
languages can behave differently on these matters??”definitely
something to look out for if you need to port code between different
.NET-based languages.
We now certainly know enough to use nullable types and predict how they??™ll behave,
but C# 2 has a sort of ???bonus track??? when it comes to syntax enhancements: the null
coalescing operator.
4.3.5 The null coalescing operator
Aside from the ? modifier, all of the rest of the C# compiler??™s tricks so far to do with
nullable types have worked with the existing syntax. However, C# 2 introduces a new
operator that can occasionally make code shorter and sweeter. It??™s called the null coalescing
operator and appears in code as ?? between its two operands. It??™s a bit like the
conditional operator but specially tweaked for nulls.
null true null true null null
null false false null null null
null null null null null null
Table 4.2 Truth table for the logical operators AND, inclusive OR, exclusive OR, and logical
negation, applied to the bool? type (continued)
x y x & y x | y x ^ y !x
129 C# 2??™s syntactic sugar for nullable types
It??™s a binary operator that evaluates first ?? second by going through the following
steps (roughly speaking):
1 Evaluate first.
2 If the result is non-null, that??™s the result of the whole expression.
3 Otherwise, evaluate second; the result then becomes the result of the whole
expression.


Pages:
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271