While you're iterating, if another thread modified the dictionary, you'll find the updated values as opposed to exception(with normal Dictionary<,>
).
If you've not yet reached the modified item, you'll eventually walk through it; otherwise, you may miss the updated value if you already visited the updated element.
From ConcurrentDictionary.GetEnumerator
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.
For example assume Thread1 is walking through the dictionary is now at position 3. If Thread2 modified element at position 7 you'll find the modified value. On the other hand, if it modified the element at position 1, you'll not notice it. (Forget the fact that dictionary is unordered; positions are used for better understanding).
I just realized that your code loops through Dictionary.Values
(Thanks @Svick for mentioning in comments); Answer above is only true if you loop through the dictionary. When you're iterating through Values
property, you're actually looping through a snapshot. If dictionary is updated in the mean time, you'll not notice it.