Why does foreach skip compile time type checking on interface types?
问题 When I use a foreach loop in C#, it appears that no compile time type checking is performed if the item type is an interface type. E.g. class SomeClass {} interface SomeInterface {} IEnumerable<SomeClass> stuff; foreach(SomeInterface obj in stuff) { // This compiles - why!? } This will happily compile and cause an exception at runtime, when it is clear at compile time this makes no sense. If I change the item type from SomeInterface to another class, then compile time type-checking is