tags:

views:

971

answers:

8

If you have an Interface IFoo and a class Bar : IFoo, why can you do the following:

List<IFoo> foo = new List<IFoo>();  
foo.Add(new Bar());

But you cannot do:

List<IFoo> foo = new List<Bar>();
+4  A: 

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.

Nathan
+1  A: 

List is the type in this case and it's not an inheritance question List<IFoo> really is different than List<Bar>. List doesn't know anythign of, or inherit the characteristics of either IFoo or Bar.

Hope that helps.

Dan Blair
A: 

List<Bar> does not inherit from List<IFoo>

David B
A: 

If you have a list of type List<IFoo> you can call list.add(new Baz()); assuming Baz implements IFoo. However you can't do that with a List<Bar>, so you can't use a List<Bar> everywhere you can use a List<IFoo>.

However since Bar implements IFoo, you can use a Bar everywhere you use IFoo, so passing a Bar to add works when it expects and IFoo.

sepp2k
+7  A: 

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.

Dustin Campbell
+1  A: 

Because a list of IFoos can contain some Bars as well, but a list of IFoos is not the same thing as a list of Bars.

Note that I used English above instead of using C#. I want to highlight that this is not a deep problem; you are just getting confused by the details of the syntax. To understand the answer you need to see beyond the syntax and think about what it actually means.

A list of IFoos can contain a Bar, because a Bar is an IFoo as well. Here we're talking about the elements of the list. The list is still a list of IFoos. We haven't changed that.

Now, the list you called foo is still a list of IFoos (more pedantically, foo is declared as a List<IFoo>). It cannot be anything else. In particular, it cannot be made into a list of Bars (List<Bar>). A list of Bar is a completely different object than a list of IFoos.

Euro Micelli
+20  A: 

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.

Adam Robinson
+1 for "this should (as in beer should be free) work" and making me smile :)
Stephen Newman
Sorry, but this answer is incorrect (possibly popular for beer reference, but not correct). Test your code, it will fail at your "//Makes sense so far" comment, because List<IFoo> is a completely different type to List<Bar> follow this link for more http://stackoverflow.com/questions/833447/why-is-this-cast-not-possible/833475#833475
Binary Worrier
Binary Worrier, Adam said it wont work and is explaining why! you read a post before commenting...
Marcom
@Binary Worrier: Yes, it will fail; that's the point of my post :) It's designed to point out that, while the code might make sense from its APPEARANCE, that there is a sane reason why it doesn't (and shouldn't) compile or run.
Adam Robinson
OK, I think I see where you're going, but it's not clear. Some noob reading this could very well think that every line of code in the answer should work except the last one, and be frustrated as hell when it doesn't. Could you make your answer more explicit? Please?
Binary Worrier
@Binary: While I feel it was pretty self-explanatory before, I've made the intention a little more explicit.
Adam Robinson
A: 

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>();
draders