Is a linq query to ConcurrentDictionary Values threadsafe?

后端 未结 4 914
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 23:48

let\'s say I have the following code:

ConcurrentDictionary myDict= new ConcurrentDictionary();

Normally

4条回答
  •  难免孤独
    2020-12-14 00:52

    As already mentioned, ConcurrentDictionary.GetEnumerator() does not represent a moment-in-time snapshot of the dictionary. However, ConcurrentDictionary.Values does produce a moment-in-time snapshot.

    Therefore the following are not equivalent:

    myDict.Any(x => !x.Value.HasPaid)
    myDict.Values.Any(x => !x.HasPaid)
    

提交回复
热议问题