简体   繁体   English

C#Dictionary是否有可能返回一个完全错误的值。

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

I have ac# dictionary in a multithreaded environment. 我在多线程环境中有ac字典。 I'm using TryGetValue for reading without lock and in the same moment the dictionary is reorganizing. 我正在使用TryGetValue进行无锁读取,同时字典正在重新组织。 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 如果你需要一个线程安全的,你可以使用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 另请参阅有关线程安全集合的Microsoft参考资料https://msdn.microsoft.com/en-us/library/dd997305(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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