Prev | Current Page 245 | Next

Jon Skeet

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


In addition, the very name ???nullable??? suggests that we should be able to assign
null to a variable of a nullable type, and we haven??™t seen that??”we??™ve always used the
default constructor of the type. In this section we??™ll see how C# 2 deals with these
issues and others.
Before we get into the details of what C# 2 provides as a language, there??™s one definition
I can finally introduce. The null value of a nullable type is the value where
HasValue returns false??”or an ???instance without a value,??? as I??™ve referred to it in section
4.2. I didn??™t use it before because it??™s specific to C#. The CLI specification
2 You??™ll learn more about static classes in chapter 7.
121 C# 2??™s syntactic sugar for nullable types
doesn??™t mention it, and the documentation for Nullable itself doesn??™t mention it.
I??™ve honored that difference by waiting until we??™re specifically talking about C# 2
itself before introducing the term.
With that out of the way, let??™s see what features C# 2 gives us, starting by reducing
the clutter in our code.
4.3.1 The ? modifier
There are some elements of syntax that may be unfamiliar at first but have an appropriate
feel to them. The conditional operator (a ? b : c) is one of them for me??”it asks
a question and then has two corresponding answers. In the same way, the ? operator
for nullable types just feels right to me.


Pages:
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257