See the following code snippet:
(IEnumerable<object>)new Dictionary<string, string>()
The above cast will throw an invalid cast exception.
Actually, IDictionary<TKey, TValue>
also indirectly implements IEnumerable<out T>
, because it also implements ICollection<T>
. That is, the whole cast should be valid.
In fact, for me it is even more strange that if I run the whole cast on a debugger watch slot, it works!
What's going on?