Hiding public interface members in IL

Consider these lines of code:

ConcurrentDictionary<string, object> error = new ConcurrentDictionary<string, object>();
error.Add("hello", "world"); //actually throws a compiler error

and

IDictionary<string, object> noError = new ConcurrentDictionary<string, object>();
noError.Add("hello", "world");

In the end, I will find out that all you have to do is change the IL to make the function Addprivate.

Now, in the spirit of the decoupled code, I would most likely use the interface, but it seems that the parallel dictionary is not too met in the method Add.

Is it really possible to use Add(I cannot view IL, so I don’t know if it is really thread safe.) Or should I use a specific type ConcurrentDictionary<TKey, TValue>and use it explicitly TryAdd.

+4
source share
2 answers

Yes, it is safe.

ConcurrentDictionary. IDictionary<TKey, TValue>.Add TryAdd , .

-, IL. #. :

void IDictionary<TKey,TValue>.Add(TKey key, TValue value) {}

, , , , , aren ' t . , ( ConcurrentDictionary, , , , ).

+8

, . ConcurrentDictionary , , , .

Add() , TryAdd(). , Add() , Microsoft , . - , ConcurrentDictionary IDictionary. , . , .

, Add(). , . , Add() - , TryAdd() . Kaboom ArgumentException, .

100% , , ? , , , , ? , , , TryAdd() false?

.

+5

All Articles