Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how ReadOnlyDictionary from .net 4.5 does not expose .Add method ? (not why)

Tags:

.net

.net-4.5

I am curious how is it possible that ReadOnlyDictionary implements IDictionary and ICollection but it does not expose the methods Add(TKey key, TValue value)

ReadOnlyDictionary<object, object> dictionary;
dictionary.Add(null, null); // compilation error 

IDictionary<object,object> i;
i = dictionary;
i.Add(null, null);// no compilation error
like image 376
Dorin Avatar asked Dec 05 '25 05:12

Dorin


1 Answers

ReadOnlyDictionary explicitly implements the Add methods (both in generic and non-generic form). Something like this:

void IDictionary.Add(Object key, Object value)

You can call it if you cast it to an IDictionary, but it will throw a NotSupportedException according to MSDN.

like image 100
vcsjones Avatar answered Dec 09 '25 14:12

vcsjones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!