Cheers Arjang.

This seems to be the best way to understand the type variance rules and why
they used the terms in/out in the implementation.

If all uses of the generic type parameter are in return (or out) positions
then you can get "out" variance. If all uses are in incoming positions (i.e.
parameters) then you can get "in" variance". You cannot have a mix of the
two.

In the case of IList<T> there is void .Add(T item) and IEnumerator<T>
GetEnumerator() so there is no valid variance for IList<T>

for IEnumerator<T> though there is only T Current { get; } so you can (and
do) get "out" variance so an IEnumerator<Banana> can get cast to
IEnumerator<IBanana>

Hope that helps.

Regards,
Mike

On Fri, Jun 4, 2010 at 2:37 PM, Arjang Assadi <arjang.ass...@gmail.com>wrote:

> True
>
> Nicely explained.
>
> Thank you Michael,
>
> On 4 June 2010 16:30, Michael Minutillo <michael.minuti...@gmail.com>
> wrote:
> > because of this:
> > class Apple : IBanana { }
> > ibanans.Add(new Apple() as IBanana);
> >
> > On Fri, Jun 4, 2010 at 2:27 PM, Arjang Assadi <arjang.ass...@gmail.com>
> > wrote:
> >>
> >> Given :
> >>
> >> interface IBanana {//code ommited}
> >>
> >> class Banana : IBanana {//code omitted}
> >>
> >> List<IBanana> banans;
> >>
> >> List<IBanana> ibanans = banans as  List<IBanana>;
> >>
> >> ibananas == null ? why?
> >>
> >> Regards
> >>
> >> Arjang
> >
> >
> >
> > --
> > Michael M. Minutillo
> > Indiscriminate Information Sponge
> > Blog: http://wolfbyte-net.blogspot.com
> >
>



-- 
Michael M. Minutillo
Indiscriminate Information Sponge
Blog: http://wolfbyte-net.blogspot.com

Reply via email to