Prev | Current Page 234 | Next

Jon Skeet

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

The more complex (and
rather more appealing) form is to have a reference type for each value type you need
in a nullable form, containing a single instance variable of that value type, and with
implicit conversion operators to and from the value type. With generics, you could do
this in one generic type??”but if you??™re using C# 2 anyway, you might as well use the
nullable types described in this chapter instead. If you??™re stuck in C# 1, you have to
create extra source code for each type you wish to wrap. This isn??™t hard to put in the
form of a template for automatic code generation, but it??™s still a burden that is best
avoided if possible.
Both of these forms have the problem that while they allow you to use null
directly, they do require objects to be created on the heap, which can lead to garbage
collection pressure if you need to use this approach very frequently, and adds memory
use due to the overheads associated with objects. For the more complex solution, you
could make the reference type mutable, which may reduce the number of instances
you need to create but could also make for some very unintuitive code.
PATTERN 3: AN EXTRA BOOLEAN FLAG
The final pattern revolves around having a normal value type value available, and
another value??”a Boolean flag??”indicating whether the value is ???real??? or whether it
should be disregarded.


Pages:
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246