Could anyone explain why the cast below fails when the implementation of IMyInterface
is a struct? I don't get why the runtime cares about the difference between a class and a struct here, given that the List is always a class.
public interface IMyInterface
{
}
public class MyClass : IMyInterface
{
}
public struct MyStruct : IMyInterface
{
}
public static void TestCast<T>()
where T : IMyInterface
{
var works = (IEnumerable<IMyInterface>)new List<MyClass>();
var invalidCastException = (IEnumerable<IMyInterface>)new List<MyStruct>();
}