The problem is that the IEnumerable
implementation on Dictionary<TKey,TValue>
lists KeyValuePairs. Foreach on an IDictionary
will use the IDictionary.GetEnumerator
function, which returns an IDictionaryEnumerator (internally this tells the IEnumerator
function for the inner class Enumerator whose type is returned). While the Cast function works on IEnumerable, which will make it return KeyValuePair.
You can write your own Cast method to get around this if you really have to use a non-common interface and DictionaryEntry
IEnumerable<DictionaryEntry> CastDE(IDictionary dict) { foreach(var item in dict) yield return item; }
source share