If you have an Interface IFoo
and a class Bar : IFoo
, why can you do the following:
List foo = new List();
Because a list of IFoo
s can contain some Bar
s as well, but a list of IFoo
s is not the same thing as a list of Bar
s.
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 IFoo
s 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 IFoo
s. We haven't changed that.
Now, the list you called foo
is still a list of IFoo
s (more pedantically, foo
is declared as a List
). It cannot be anything else. In particular, it cannot be made into a list of Bar
s (List
). A list of Bar
is a completely different object than a list of IFoo
s.