Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding public members of an interface in IL

Tags:

c#

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");

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.

like image 932
dbarnes Avatar asked Dec 12 '22 00:12

dbarnes


1 Answers

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).

like image 95
Erik Avatar answered Dec 29 '22 14:12

Erik