I've been reading C# newbie List<Interface> question and can understand why this first example works, but not the second. Why is the first good, but the second piece of code fails to compile.
First the good code:
interface IFoo {}
class Foo : IFoo { }
class Bar : IFoo { }
var list = new List<IFoo>();
list.Add(new Foo());
list.Add(new Bar());
Now for the code which introduces generics
interface IZar<TFoo> where TFoo : IFoo { }
class ZarFoo : IZar<Foo> { }
class ZarBar : IZar<Bar> { }
var list2 = new List<IZar<IFoo>>();
list2.Add(new ZarFoo());
list2.Add(new ZarBar());
This is failing to compile because ZarFoo cannot be converted to IZar when surely it should be able to as it implements IZar where Foo : IFoo?