views:

48

answers:

2

IDictionary<TKey, TValue> inherits from IEnumerable<KeyValuePair<TKey, TValue>>, but IDictionary for some reason doesn't inherit from IEnumerable<DictionaryEntry>. I wonder why? I hate to write this ugly .OfType<DictionaryEntry>() every time when I need to make a query against an IDictionary.

+2  A: 

Because IDictionary is an older (pre-generics) interface. Changing it would break existing code. It is a legacy.

So the underlying issue is why are you (still) using IDictionary based classes, and can't you upgrade?

Henk Holterman
+1  A: 

Making that change would break every existing implementation of the interface (because they would suddenly be missing some of the methods the interface says they must have).

Furthermore, IDictionary only exists for legacy compatibility from the time before generics. You shouldn't be using it in new code, unless absolutely necessary. If you're actually working with object types, you can use IDictionary<object, object> instead.

Strilanc