ConcurrentDictionary TryGetValue与[]。 []仍然是线程安全的吗? [英] ConcurrentDictionary TryGetValue vs []. Is [] still thread-safe?
问题描述
我有以下 ConcurrentDictionary
:
ConcurrentDictionary<Guid, Session> sessions;
我知道 sessions.TryGetValue(key,out session)
是线程安全的,但是我的问题是 sessions [key]
也是线程安全的吗?
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)
根据是否能够获取值而返回true或false。
sessions.TryGetValue(key, out session)
returns true or false depending on whether it was able to get the value or not.
如果无法获取值, sessions [key]
会返回 null
吗?我会这样认为。谁能证实或对此有更多了解?谢谢。
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.
推荐答案
它是线程安全的,但不会返回 null
。
It is thread-safe, but it will not return null
.
文档明确指出:
异常
KeyNotFoundException
检索属性,并执行键操作集合中不存在。
Exceptions
KeyNotFoundException
The property is retrieved and key does not exist in the collection.
这篇关于ConcurrentDictionary TryGetValue与[]。 []仍然是线程安全的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!