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");
I eventually figure out that all you have to do is change the IL to make the Add
function private.
Now in the spirit of decoupled code I'd most likely use the Interface but it seems that Concurrent dictionary isn't too found of the Add
method.
Is it safe to really use Add
(I can't view the IL so I don't know if it's really thread safe.)? Or should I use the concrete type of ConcurrentDictionary<TKey, TValue>
and explicitly use TryAdd
.
Yes, it's safe.
Have a look at the reference source for ConcurrentDictionary. The method IDictionary<TKey, TValue>.Add
simply calls TryAdd
and throws an exception if the key already exists.
The hiding of members of an interface is not something that requires IL modifications to be done. It can be done through explicit interface implementation in C#. This is done by leaving off the access modifier of the method and prefixing the method name with the interface name:
void IDictionary<TKey,TValue>.Add(TKey key, TValue value) {}
There are various reasons for doing this, maybe you don't want to clutter the concrete interface, or you want consumers of your class to be explicit about what method they are using if the name of the methods on the interface aren't specific enough. Also, it allows you to provide separate implementations for methods on different interfaces with the same signature (not really an issue for ConcurrentDictionary
I think, but the capability is there if you need it in your own classes).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With