I have the following code:
IEnumerable<IList<MyClass>> myData = //...getMyData
foreach (MyClass o in myData)
{
// do something
}
It compiles, runs and obviously I get an System.InvalidCastException
.
Why does the compiler not complain? MyClass
is a simple bean, no extensions.
Edit 1:
As suggested by David switching the type from IList
to List
the compiler complains
Edit 2:
I've understood that the behaviour is as specified in the C# Language definition. However, I don't understand why such a cast/conversion is allowed, since at runtime I always get an InvalidCastException. I opened this in order to go deeper.