Prev | Current Page 251 | Next

Jon Skeet

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

Likewise we??™ve seen wrapping and unwrapping, but other conversions can be
used with some types. This section explains what??™s available. I??™m afraid it??™s pretty much
impossible to make this kind of topic genuinely exciting, but carefully designed features
like these are what make C# a pleasant language to work with in the long run.
Don??™t worry if not all of it sinks in the first time: just remember that the details are here
if you need to reference them in the middle of a coding session.
The ???executive summary??? is that if there is an operator or conversion available on
a non-nullable value type, and that operator or conversion only involves other nonnullable
value types, then the nullable value type also has the same operator or conversion
available, usually converting the non-nullable value types into their nullable
equivalents. To give a more concrete example, there??™s an implicit conversion from int
to long, and that means there??™s also an implicit conversion from int? to long? that
behaves in the obvious manner.
Unfortunately, although that broad description gives the right general idea, the
exact rules are slightly more complicated. Each one is simple, but there are quite a few
of them. It??™s worth knowing about them because otherwise you may well end up staring
at a compiler error or warning for a while, wondering why it believes you??™re trying
to make a conversion that you never intended in the first place.


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