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

2

6

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.

Crumble answered 18/6, 2010 at 10:51 Comment(0)
P
3

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.

Polly answered 18/6, 2010 at 11:0 Comment(0)
N
4

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?

Nephrotomy answered 18/6, 2010 at 11:0 Comment(0)
P
3

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.

Polly answered 18/6, 2010 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.