Prev | Current Page 204 | Next

Jon Skeet

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

If the key is not present, TryGetValue will set
the output parameter to the default value of TValue and return false. This avoids
having to search for the key twice, while still allowing the caller to distinguish between
the situation where a key isn??™t present at all, and the one where it??™s present but its associated
value is the default value of TValue. Making the indexer throw an exception is
of more debatable merit, but it does make it very clear when a lookup has failed
instead of masking the failure by returning a potentially valid value.
100 CHAPTER 3 Parameterized typing with generics
Just as with List, there is no way of obtaining a synchronized Dictionary<,>,
nor does it implement ICloneable. The dictionary equivalent of Synchronized-
Collection is SynchronizedKeyedCollection (which in fact derives from
SynchronizedCollection).
With the lack of additional functionality, another example of Dictionary<,>
would be relatively pointless. Let??™s move on to two types that are closely related to
each other: Queue and Stack.
3.5.3 Queue and Stack
The generic queue and stack classes are essentially the same as their nongeneric counterparts.
The same features are ???missing??? from the generic versions as with the other
collections??”lack of cloning, and no way of creating a synchronized version. As
before, the two types are closely related??”both act as lists that don??™t allow random
access, instead only allowing elements to be removed in a certain order.


Pages:
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216