Open Korporal opened 5 years ago
FWIW, the answer is "no".
All methods internally lock the SyncRoot
, including GetEnumerator()
, but the actual enumeration is not thread-safe. Calling code will need to externally lock SyncRoot
during enumeration.
(Note that it is possible to write a collection class that does have thread-safe enumeration automatically -- and it's very easy to do using C#2 iterators. This particular class does not do so, however, possibly in part because it predates that language feature.)
The docs about this refer to "thread safe" yet say absolutely nothing about whether multiple threads may enumerate the elements in such a way that no thread ever gets the same element.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.