IDictionary<TKey, TValue>
actually extends IEnumerable<KeyValuePair<TKey, TValue>>
. This is why you can use LINQ operators on an IDictionary<TKey, TValue>
in the first place.
However, LINQ operators return IEnumerable<T>
which are meant to provide deferred execution, meaning the results aren't actually generated until you start iterating through the IEnumerable<T>
.
The IEnumerable<T>
implementation which is provided by IDictionary<TKey, TValue>
comes by way of the ICollection<T>
interface (where T
is a KeyValuePair<TKey, TValue>
), which means that if LINQ were to return IDictionary<TKey, TValue>
instead of IEnumerable<KeyValuePair<TKey, TValue>>
then it would have to materialize the list, violating it's principals (hence the IEnumerable<KeyValuePair<TKey, TValue>>
return value).
Of course, the way around it is to call the ToDictionary
extension method on the Enumerable class
(as others have mentioned), but a little back-story never hurts.