Prev | Current Page 247 | Next

Jon Skeet

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

From
now on, we??™ll use the ? modifier in all the examples??”it??™s neater, and it??™s arguably the
idiomatic way to use nullable types in C# 2. However, you may feel that it??™s too easy to
miss when reading the code, in which case there??™s certainly nothing to stop you from
using the longer syntax. You may wish to compare the listings in this section and the
previous one to see which you find clearer.
Listing 4.3 The same code as listing 4.2 but using the ? modifier
122 CHAPTER 4 Saying nothing with nullable types
Given that the C# 2 specification defines the null value, it would be pretty odd if we
couldn??™t use the null literal we??™ve already got in the language in order to represent it.
Fortunately we can, as our next section will show.
4.3.2 Assigning and comparing with null
A very concise author could cover this whole section in a single sentence: ???The C#
compiler allows the use of null to represent the null value of a nullable type in both
comparisons and assignments.??? I prefer to show you what it means in real code, as well
as think about why the language has been given this feature.
You may have felt a bit uncomfortable every time we??™ve used the default constructor
of Nullable. It achieves the desired behavior, but it doesn??™t express the reason
we want to do it??”it doesn??™t leave the right impression with the reader.


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