Prev | Current Page 202 | Next

Jon Skeet

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

GetRange (0, list.Count) or new List(list)
FixedSize None
Repeat for loop or write a replacement generic method
SetRange for loop or write a replacement generic method
Synchronized SynchronizedCollection
99 Generic collection classes in .NET 2.0
calls. To make those operations thread-safe, the collection needs to be locked for the
duration of the operation. (It requires cooperation from other code using the same
collection, of course.) In short, the Synchronized method gave the appearance of
safety without the reality. It??™s better not to give the wrong impression in the first
place??”developers just have to be careful when working with collections accessed in
multiple threads. SynchronizedCollection performs broadly the same role as a
synchronized ArrayList. I would argue that it??™s still not a good idea to use this, for the
reasons outlined in this paragraph??”the safety provided is largely illusory. Ironically,
this would be a great collection to support a ForEach method, where it could automatically
hold the lock for the duration of the iteration over the collection??”but there??™s
no such method.
That completes our coverage of List. The next collection under the microscope
is Dictionary, which we??™ve already seen so much of.
3.5.2 Dictionary
There is less to say about Dictionary (just called Dictionary<,> for
the rest of this section, for simplicity) than there was about List, although it??™s
another heavily used type.


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