Why doesn't the parallel dictionary have a visible Add () method?

I'm just wondering how it is possible that ConcurrentDictionary does not have the Add method that is visible in Visual Studio. I only seem to get TryX Methods like TryAdd, TryUpdate etc.

I see that ConcurrentDictionary implements IDictionary , and if I drop it on an IDictionary , I return the add method.

I looked through the class through iLSpy, and I see that the add method is fully implemented and actually calls the Concurrent TryAdd method under the hood.

I expected to see some attribute of the Add method to push it, but I can’t see anything.

Was this done in the Microsoft IDE to hide the Add method by default?

If anyone can shed light on this, it will be appreciated

+4
source share
2 answers

This is due to the explicit implementation of the interface . See http://msdn.microsoft.com/en-us/library/aa288461(v=vs.71).aspx

+6
source

They discourage the use of the Add method because the method throws an exception if the key is already present in the dictionary. For most dictionaries, a developer can write code in such a way as to ensure that an exception will not be thrown in any normal scenario. However, to perform this operation ( Contains followed by Add ) with a parallel dictionary, you will need to use exclusive locks in dictionary access methods, which is detrimental to the whole purpose of the parallel dictionary.

TryAdd combines the Contains and Add checks without requiring a dictionary lock, and allows you to write code again that will not throw an exception in normal scripts.

+3
source

All Articles