Yes, take a look at the System.Collections.Concurrent
namespace in .NET 4.0.
Note that for some of the collections in this namespace (e.g., ConcurrentQueue<T>
), this works by only exposing an enumerator on a "snapshot" of the collection in question.
From the MSDN documentation on ConcurrentQueue<T>
:
The enumeration represents a
moment-in-time snapshot of the
contents of the queue. It does not
reflect any updates to the collection
after GetEnumerator was called. The
enumerator is safe to use concurrently
with reads from and writes to the
queue.
This is not the case for all of the collections, though. ConcurrentDictionary<TKey, TValue>
, for instance, gives you an enumerator that maintains updates to the underlying collection between calls to MoveNext
.
From the MSDN documentation on ConcurrentDictionary<TKey, TValue>
:
The enumerator returned from the
dictionary is safe to use concurrently
with reads and writes to the
dictionary, however it does not
represent a moment-in-time snapshot of
the dictionary. The contents exposed
through the enumerator may contain
modifications made to the dictionary
after GetEnumerator was called.
If you don't have 4.0, then I think the others are right and there is no such collection provided by .NET. You can always build your own, however, by doing the same thing ConcurrentQueue<T>
does (iterate over a snapshot).