Quick sanity check: covariance, contravariance, lack of variance inC#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • raylopez99

    #1

    Quick sanity check: covariance, contravariance, lack of variance inC#

    What are these terms in my header anyway?

    But more importantly for me, look at this code:

    Lack of variance:
    why a List<Bananaisn’ t a List<Fruit>

    Many people initially expect the following code to compile:

    List<Bananabana nas = new List<Banana>();
    List<Fruitfruit = bananas;

    //however, it fails to compile (from Jon Skeet's cheat sheet)

    However, this should work, right??:

    List <FruitmybaseFru it = new List <Fruit>();

    class Banana: Fruit {}

    Banana myBanana = new Banana(); //note, this is NOT a list, but an
    instantiation of a derived class Banana having base class Fruit

    mybaseFruit.Add (myBanana); //should work, right? Otherwise what's the
    point of inheritance?

    RL





  • Stefan Hoffmann

    #2
    Re: Quick sanity check: covariance, contravariance, lack of variancein C#

    hi Ray,

    raylopez99 wrote:
    Many people initially expect the following code to compile:
    >
    List<Bananabana nas = new List<Banana>();
    List<Fruitfruit = bananas;
    This should work:

    List<Fruitfruit = (Fruit)bananas;


    mfG
    --stefan <--

    Comment

    • Marc Gravell

      #3
      Re: Quick sanity check: covariance, contravariance, lack of variancein C#

      However, this should work, right??:

      And it does. Was there a question?

      Marc

      Comment

      • Marc Gravell

        #4
        Re: Quick sanity check: covariance, contravariance, lack of variancein C#

        This should work:

        You cannot cast between the two lists (assuming you meant (List<Fruit>),
        not (Fruit)).

        The closest you can get is a generic method:

        static void AddFruit<T>(ILi st<Tlist, T item) where T : Fruit
        {
        list.Add(item);
        }

        (Add isn't a good choice because the caller could do that moer
        conveniently directly, but searching etc would work in the above, using
        properties from the base Fruit class)

        Marc

        Comment

        • Stefan Hoffmann

          #5
          Re: Quick sanity check: covariance, contravariance, lack of variancein C#

          hi Marc,

          Marc Gravell wrote:
          You cannot cast between the two lists (assuming you meant (List<Fruit>),
          not (Fruit)).
          Indeed I meant (Fruit), I have not read carefully enough.
          The closest you can get is a generic method:
          >
          static void AddFruit<T>(ILi st<Tlist, T item) where T : Fruit
          {
          list.Add(item);
          }
          Maybe an Adapter is a solution, like ArrayList.Adapt er(). Which is in
          fact a kind of your supposed generic method.


          mfG
          --stefan <--

          Comment

          Working...