"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in
news:11*********************@57g2000hsv.googlegrou ps.com:
On Sep 6, 8:43 am, Peter K <xdz...@hotmail.comwrote:
<snip>
>Why is it I cannot perform a call like this:
List<Userusers = new List<User>();
AddUserList(users);
I would have naively thought that as User implements IUser, I could
pass a list of User objects to a method which expects a list of IUser
objects (as a User is an IUser).
Nope. Suppose the AddUserList implementation was this:
users.Add (new OtherIUserImplementation());
At that point your List<Userwould contain a reference to something
which isn't a User.
I'm not following you here... If I have a list defined
List<Userusers
then I obviously can't add an OtherUser object, because the list is only
for User objects.
But If I had this list:
List<IUserusers
I would have thought I could add a User:IUser and an OtherUser:IUser, as
the list is defined as accepting IUser objects, and both my objects are
IUser objects.
But my question was about supplying the list to a method, not about
adding objects to the list (maybe it's the same question?).
Basically, generics (at least as exposed in C#) don't support
covariance/contravariance. This gives more compile-time safety at the
cost of some convenience.
See http://blogs.msdn.com/rmbyers/archiv...16/375079.aspx for
more information.
I'll have a look.
Thanks,
Peter