Generic list and inheritance

  • Thread starter Thread starter santimbs
  • Start date Start date
S

santimbs

Hi everyone,

Lets say I have:

interface ICar
{
}

class Mercedes : ICar
{
}

Why can't I assign:

IList<ICar> cars = new List<Mercedes>();

thanks in advance,
Santi
 
Nicholas Paldino said:
Santi,

Here is a response to a previous thread which answers this:

http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/msg/7c964a0781c7ec65

Basically, because while the CLR supports covariance with Generics, C#
doesn't expose that functionality.

IList, which supports both input and output, cannot be covariant (or
contravariant).
Here is the rest of the thread with more details:

http://groups.google.com/group/micr...no+generic+covariance&rnum=2#55824f601c2e11b6


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi everyone,

Lets say I have:

interface ICar
{
}

class Mercedes : ICar
{
}

Why can't I assign:

IList<ICar> cars = new List<Mercedes>();

thanks in advance,
Santi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top