문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top