private static void TestStructInterface()
{
IFoo foo1 = new FooClass(); // works
IFoo foo2 = new FooStruct(); // works
IEnumerable<IFoo> foos1 = new List<FooClass>(); // works
IEnumerable<IFoo> foos2 = new List<FooStruct>(); // compiler error
}
interface IFoo
{
string Thing { get; set; }
}
class FooClass : IFoo
{
public string Thing { get; set; }
}
struct FooStruct : IFoo
{
public string Thing { get; set; }
}
The compiler complains:
Cannot implicitly convert type 'System.Collections.Generic.List<Tests.Program.FooStruct>' to 'System.Collections.Generic.IEnumerable<Tests.Program.IFoo>'. An explicit conversion exists (are you missing a cast?)
Why?
Why is there a difference between classes and structs?
Any workarounds?