Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 600031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:33:45+00:00 2026-05-13T16:33:45+00:00

I am having trouble understanding the difference between covariance and contravariance.

  • 0

I am having trouble understanding the difference between covariance and contravariance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:33:45+00:00Added an answer on May 13, 2026 at 4:33 pm

    The question is “what is the difference between covariance and contravariance?”

    Covariance and contravariance are properties of a mapping function that associates one member of a set with another. More specifically, a mapping can be covariant or contravariant with respect to a relation on that set.

    Consider the following two subsets of the set of all C# types. First:

    { Animal, 
      Tiger, 
      Fruit, 
      Banana }.
    

    And second, this clearly related set:

    { IEnumerable<Animal>, 
      IEnumerable<Tiger>, 
      IEnumerable<Fruit>, 
      IEnumerable<Banana> }
    

    There is a mapping operation from the first set to the second set. That is, for each T in the first set, the corresponding type in the second set is IEnumerable<T>. Or, in short form, the mapping is T → IE<T>. Notice that this is a “thin arrow”.

    With me so far?

    Now let’s consider a relation. There is an assignment compatibility relationship between pairs of types in the first set. A value of type Tiger can be assigned to a variable of type Animal, so these types are said to be “assignment compatible”. Let’s write “a value of type X can be assigned to a variable of type Y” in a shorter form: X ⇒ Y. Notice that this is a “fat arrow”.

    So in our first subset, here are all the assignment compatibility relationships:

    Tiger  ⇒ Tiger
    Tiger  ⇒ Animal
    Animal ⇒ Animal
    Banana ⇒ Banana
    Banana ⇒ Fruit
    Fruit  ⇒ Fruit
    

    In C# 4, which supports covariant assignment compatibility of certain interfaces, there is an assignment compatibility relationship between pairs of types in the second set:

    IE<Tiger>  ⇒ IE<Tiger>
    IE<Tiger>  ⇒ IE<Animal>
    IE<Animal> ⇒ IE<Animal>
    IE<Banana> ⇒ IE<Banana>
    IE<Banana> ⇒ IE<Fruit>
    IE<Fruit>  ⇒ IE<Fruit>
    

    Notice that the mapping T → IE<T> preserves the existence and direction of assignment compatibility. That is, if X ⇒ Y, then it is also true that IE<X> ⇒ IE<Y>.

    If we have two things on either side of a fat arrow, then we can replace both sides with something on the right hand side of a corresponding thin arrow.

    A mapping which has this property with respect to a particular relation is called a “covariant mapping”. This should make sense: a sequence of Tigers can be used where a sequence of Animals is needed, but the opposite is not true. A sequence of animals cannot necessarily be used where a sequence of Tigers is needed.

    That’s covariance. Now consider this subset of the set of all types:

    { IComparable<Tiger>, 
      IComparable<Animal>, 
      IComparable<Fruit>, 
      IComparable<Banana> }
    

    now we have the mapping from the first set to the third set T → IC<T>.

    In C# 4:

    IC<Tiger>  ⇒ IC<Tiger>
    IC<Animal> ⇒ IC<Tiger>     Backwards!
    IC<Animal> ⇒ IC<Animal>
    IC<Banana> ⇒ IC<Banana>
    IC<Fruit>  ⇒ IC<Banana>     Backwards!
    IC<Fruit>  ⇒ IC<Fruit>
    

    That is, the mapping T → IC<T> has preserved the existence but reversed the direction of assignment compatibility. That is, if X ⇒ Y, then IC<X> ⇐ IC<Y>.

    A mapping which preserves but reverses a relation is called a contravariant mapping.

    Again, this should be clearly correct. A device which can compare two Animals can also compare two Tigers, but a device which can compare two Tigers cannot necessarily compare any two Animals.

    So that’s the difference between covariance and contravariance in C# 4. Covariance preserves the direction of assignability. Contravariance reverses it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble understanding the difference between a 32 bit processor and 64
I am having trouble with understanding the difference between ADDNES and ADDSNE in ARM
I am having trouble understanding the difference between these two commands that in my
I am learning algorithm analysis. I am having trouble understanding the difference between O,
I am having some trouble understanding the difference between @OneToMany and @ManyToMany . When
I'm having trouble understanding what causes this difference between C++ and C#. First we
I'm having trouble understanding the difference between private and protected members in a C++
I'm having trouble understanding the difference between the jquery-rails ruby gem & jRails .
I'm having trouble understanding the difference between text() and node() . From what I
I'm having some trouble understanding the difference between these two code segments: I allocate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.