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.
Add
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.
ConcurrentDictionary<TKey, TValue>
TryAdd
Yes, it is safe.
ConcurrentDictionary. IDictionary<TKey, TValue>.Add TryAdd , .
IDictionary<TKey, TValue>.Add
-, IL. #. :
void IDictionary<TKey,TValue>.Add(TKey key, TValue value) {}
, , , , , aren ' t . , ( ConcurrentDictionary, , , , ).
ConcurrentDictionary
, . ConcurrentDictionary , , , .
Add() , TryAdd(). , Add() , Microsoft , . - , ConcurrentDictionary IDictionary. , . , .
, Add(). , . , Add() - , TryAdd() . Kaboom ArgumentException, .
100% , , ? , , , , ? , , , TryAdd() false?
.