Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Generic list and inheritance

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
santimbs@gmail.com - 26 Sep 2007 17:39 GMT
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
Doug Semler - 26 Sep 2007 17:54 GMT
On Sep 26, 12:39 pm, "santi...@gmail.com" <santi...@gmail.com> wrote:
> Hi everyone,
>
[quoted text clipped - 14 lines]
> thanks in advance,
> Santi

MSDN and Rick Byers can explain generic type invariance better than I
could :)

http://msdn2.microsoft.com/en-us/library/ms228359(vs.80).aspx
http://blogs.msdn.com/rmbyers/archive/2005/02/16/375079.aspx
http://blogs.msdn.com/rmbyers/archive/2006/06/01/613690.aspx
Nicholas Paldino [.NET/C# MVP] - 26 Sep 2007 18:00 GMT
Santi,

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

http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/msg/7c96
4a0781c7ec65


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

   Here is the rest of the thread with more details:

http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_t
hread/thread/b47879b2fecdf61a/55824f601c2e11b6?lnk=st&q=nicholas+paldino+generic
+covariance&rnum=2#55824f601c2e11b6


Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi everyone,
>
[quoted text clipped - 14 lines]
> thanks in advance,
> Santi
santimbs@gmail.com - 26 Sep 2007 22:28 GMT
Very explanatory  links, thank you.
Ben Voigt [C++ MVP] - 26 Sep 2007 23:11 GMT
> Santi,
>
[quoted text clipped - 4 lines]
>    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:
>
[quoted text clipped - 18 lines]
>> thanks in advance,
>> Santi

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.