C# List<Interface>: why you cannot do `List<IFoo> foo = new List<Bar>();`
Solution 1:
At a casual glance, it appears that this should (as in beer should be free) work. However, a quick sanity check shows us why it can't. Bear in mind that the following code will not compile. It's intended to show why it isn't allowed to, even though it looks alright up until a point.
public interface IFoo { }
public class Bar : IFoo { }
public class Zed : IFoo { }
//.....
List<IFoo> myList = new List<Bar>(); // makes sense so far
myList.Add(new Bar()); // OK, since Bar implements IFoo
myList.Add(new Zed()); // aaah! Now we see why.
//.....
myList
is a List<IFoo>
, meaning it can take any instance of IFoo
. However, this conflicts with the fact that it was instantiated as List<Bar>
. Since having a List<IFoo>
means that I could add a new instance of Zed
, we can't allow that since the underlying list is actually List<Bar>
, which can't accommodate a Zed
.
Solution 2:
The reason is that C# does not support co- and contravariance for generics in C# 3.0 or earlier releases. This is being implemented in C# 4.0, so you'll be able to do the following:
IEnumerable<IFoo> foo = new List<Bar>();
Note that in C# 4.0, you can cast to IEnumerable<IFoo>, but you won't be be able cast to List<IFoo>. The reason is due to type safety, if you were able to cast a List<Bar> to List<IFoo> you would be able to add other IFoo implementors to the list, breaking type safety.
For more background on covariance and contravariance in C#, Eric Lippert has a nice blog series.
Solution 3:
If you need to convert a list to a list of a base class or interface you can do this:
using System.Linq;
---
List<Bar> bar = new List<Bar>();
bar.add(new Bar());
List<IFoo> foo = bar.OfType<IFoo>().ToList<IFoo>();
Solution 4:
It is to do with the creation of the List, you have specified the T to be IFoo therefore you cannot instantiate it as a Bar since they are different types, even though Bar supports IFoo.