Thread Safety with Dictionary

后端 未结 3 964
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-13 20:23

If I have a

Dictionary myDic = new Dictionary
//Populate dictionary

One thread does

3条回答
  •  一向
    一向 (楼主)
    2021-02-13 20:34

    You will only get InvalidOperationException: Collection was modified if you enumerate the dictionary while modifying.

    However, that is not thread-safe.
    If one of those operations causes the dictionary to resize, the other one may get lost.
    Instead, use ConcurrentDictionary.

提交回复
热议问题