简体   繁体   中英

Is it possible C# Dictionary to return a completly wrong value.

I have ac# dictionary in a multithreaded environment. I'm using TryGetValue for reading without lock and in the same moment the dictionary is reorganizing. Is it possible to get a completly different value not related to this key? I mean not just a value that is not up-to-date but a value that is totaly wrong corresponding to a different key.

Short answer: yes!

Long answer: The dictionary is not thread safe by default. If you need a thread safe one, you can use the ConcurrentDictionary https://msdn.microsoft.com/en-us/library/dd287191(v=vs.110).aspx

Also have a look at the Microsoft reference on thread safe collections https://msdn.microsoft.com/en-us/library/dd997305(v=vs.110).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM