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 8738771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:47:52+00:00 2026-06-13T10:47:52+00:00

C# 4.0 has extended the co and contravariance further for generic types and interfaces.

  • 0

C# 4.0 has extended the co and contravariance further for generic types and interfaces. Some interfaces (like IEnumerable<T>) are covariants, so I can do things like:

IEnumerable<object> ie = new List<string>();

but what about this line? I got a compile-time error

List<Object> list = new List<String>();
//Cannot implicitly convert type List<string>' to List<object>'

I mean, if List<T> implement IEnumerable<T> why List<T> is still invariant? Is out there a good counterexample that explain why this should not be allowed in C#?

  • 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-06-13T10:47:53+00:00Added an answer on June 13, 2026 at 10:47 am

    Firstly, classes are always invariant in C#. You can’t declare a class like this:

    // Invalid
    public class Foo<out T>
    

    Secondly – and more importantly for the example you’ve given – List<T> couldn’t be declared to be covariant or contravariant in T anyway, as it has members both accepting and returning values of type T.

    Imagine if it were covariant. Then you could write this (for the obvious Fruit class hierarchy):

    List<Banana> bunchOfBananas = new List<Banana>();
    // This would be valid if List<T> were covariant in T
    List<Fruit> fruitBowl = bunchOfBananas;
    fruitBowl.Add(new Apple());
    Banana banana = bunchOfBananas[0];
    

    What would you expect that last line to do? Fundamentally, you shouldn’t be able to add an Apple reference to an object whose actual execution-time type is List<Banana>. If you add an apple to a bunch of bananas, it falls off. Believe me, I’ve tried.

    The last line should be safe in terms of types – the only values within a List<Banana> should be null or references to instances of Banana or a subclass.

    Now as for why classes can’t be covariant even when they could logically be… I believe that introduces problems at the implementation level, and would also be very restrictive at the programming level as well. For example, consider this:

    public class Foo<out T> // Imagine if this were valid
    {
        private T value;
    
        public T Value { get { return value; } }
    
        public Foo(T value)
        {
            this.value = value;
        }
    }
    

    That would still probably have to be invalid – the variable is still writable, meaning it counts as an “in” slot. You’d have to make every variable of type T read-only… and that’s just for starters. I strongly suspect that there would be deeper problems.

    In terms of pure pragmatism, the CLR has supported delegate and interface variance from v2 – C# 4 just introduced the syntax to expose the feature. I don’t believe the CLR has ever supported generic class variance.

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

Sidebar

Related Questions

Synopsis: Each User account has a UserProfile to hold extended info like phone numbers,
I'm having some trouble figuring out how to require a class has extended an
Joomla 1.5 has JTable, which can be extended to act as an Active Record
I have a object that is extended from arraycollection. This object has to access
How can I detect if a windows mobile device has the keyboard extended? I
I have a Custom DirectShow Video Renderer Filter which has some extended features over
Basically I want to check the extended permissions the user has granted my app.
An application that I support has recently begun experiencing extended periods of time required
When an instance of an object extends a module and extended is called on
If I have an object: var myobj={name: 'Some Value', id: 'my id', address: 'my

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.