ConcurrentDictionary TryGetValue vs []. Is [] still thread-safe?

坚强是说给别人听的谎言 提交于 2020-03-18 03:37:07

问题


I have the following ConcurrentDictionary:

ConcurrentDictionary<Guid, Session> sessions;

I know that sessions.TryGetValue(key, out session) is thread-safe, but my question is if sessions[key] is also thread-safe?

sessions.TryGetValue(key, out session) returns true or false depending on whether it was able to get the value or not.

Will sessions[key] return null if it is unable to get the value? I would think so. Can anyone confirm or shed more light on this? Thanks.


回答1:


It is thread-safe, but it will not return null.

The documentation clearly states:

Exceptions

KeyNotFoundException
The property is retrieved and key does not exist in the collection.



来源:https://stackoverflow.com/questions/17088363/concurrentdictionary-trygetvalue-vs-is-still-thread-safe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!