Throw from ConcurrentDictionary to IDictionary

Is disabling a thread-safe implementation different from ConcurrentDictionary to an IDictionary, since IDictionary does not have the GetOrAdd and AddOrUpdate methods?

+6
source share
5 answers

The resulting object will still be a parallel dictionary. Calls such as Add or Remove use the underlying implementation of TryAdd and TryRemove (which are thread safe). Dropping an object by another type does not change the object itself.

In addition, for clarification, you can use tools like ILSpy to find out what the implementation of the standard IDictionary methods is and whether they will still be thread safe.

+10
source

IDictionary is just an interface. If you apply it, the result will be an implementation of ConcurrentDictionary, the missing GetOrAdd and AddOrUpdate .

Presumably, you can still use the Item and Add and ContainsKey property (instead of the GetOrAdd and AddOrUpdate ), and your cast object will still be thread safe (since the underlying implementation is ConcurrentDictionary ).

+6
source

The interface does not affect the implementation. It simply does not expose some of the ConcurrentDictionary methods.

You can find this one or this one for understanding the interfaces.

+1
source

It would be interesting to look at a large ConcurrentDictionary object through an IDictionary shaped keyhole - you could only see the IDictionary form, but it would still be ConcurrentDictionary .

0
source

Short answer no.

You control an object through an interface and therefore still use a specific implementation. You do not lose any functionality and your methods. They are simply unavailable.

On the side of the note, you need to explicitly use it when downcasting, there is no need to explicitly cast when raising - it's always safe to do this.

-1
source

Source: https://habr.com/ru/post/912967/


All Articles