Search code examples
.netgenericsienumerableidictionarybase-class-library

Why does not IDictionary (non-generic) inherit from IEnumerable<DictionaryEntry>?


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.


Solution

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