is locking necessary for Dictionary lookup?

后端 未结 9 1906
太阳男子
太阳男子 2020-12-16 16:52
lock(dictionaryX)
{
   dictionaryX.TryGetValue(key, out value);
}

is locking necessary while doing lookups to a Dictionary ?

THe program is

9条回答
  •  孤城傲影
    2020-12-16 17:29

    If you are on .Net 4, you can replace with ConcurrentDictionary to do this safely. There are other similar collections, preferred when you need multithreaded access, in the System.Collection.Concurrent namespace.

    Don't use roll-your-own locking if this is an option for you.

提交回复
热议问题