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

The Archive Base Latest Questions

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

Generic Variance in C# 4.0 has been implemented in such a way that it’s

  • 0

Generic Variance in C# 4.0 has been implemented in such a way that it’s possible to write the following without an exception (which is what would happen in C# 3.0):

 List<int> intList = new List<int>();
 List<object> objectList = intList; 

[Example non-functional: See Jon Skeet’s answer]

I recently attended a conference where Jon Skeet gave an excellent overview of Generic Variance, but I’m not sure I’m completely getting it – I understand the significance of the in and out key words when it comes to contra and co-variance, but I’m curious to what happens behind the scenes.

What does the CLR see when this code is executed? Is it implicitly converting the List<int> to List<object> or is it simply built in that we can now convert between derived types to parent types?

Out of interest, why wasn’t this introduced in previous versions and what’s the main benefit – ie real world usage?

More info on this post for Generic Variance (but question is extremely outdated, looking for real, up-to-date information)

  • 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-13T15:46:00+00:00Added an answer on May 13, 2026 at 3:46 pm

    No, your example wouldn’t work for three reasons:

    • Classes (such as List<T>) are invariant; only delegates and interfaces are variant
    • For variance to work, the interface has to only use the type parameter in one direction (in for contravariance, out for covariance)
    • Value types aren’t supported as type arguments for variance – so there’s no converstion from IEnumerable<int> to IEnumerable<object> for example

    (The code fails to compile in both C# 3.0 and 4.0 – there’s no exception.)

    So this would work:

    IEnumerable<string> strings = new List<string>();
    IEnumerable<object> objects = strings;
    

    The CLR just uses the reference, unchanged – no new objects are created. So if you called objects.GetType() you’d still get List<string>.

    I believe it wasn’t introduced earlier because the language designers still had to work out the details of how to expose it – it’s been in the CLR since v2.

    The benefits are the same as other times where you want to be able to use one type as another. To use the same example I used last Saturday, if you’ve got something implements IComparer<Shape> to compare shapes by area, it’s crazy that you can’t use that to sort a List<Circle> – if it can compare any two shapes, it can certainly compare any two circles. As of C# 4, there’d be a contravariant conversion from IComparer<Shape> to IComparer<Circle> so you could call circles.Sort(areaComparer).

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

Sidebar

Related Questions

any generic way to trace/log values of all local variables when an exception occurs
There must be a generic way to transform some hierachical XML such as: <element1
Is there a generic way, without creating and managing your own CLR host, to
I have a generic class that should allow any type, primitive or otherwise. The
I've got a generic<> function that takes a linq query ('items') and enumerates through
I have a generic class that I'm trying to implement implicit type casting for.
I've got a generic dictionary Dictionary<string, T> that I would like to essentially make
I am using a hierarchy of generic collection classes that derive from an abstract
So now that we have generic Covariance and Contravariance on interfaces and delegates in
Is there a way to reflect on an interface to detect variance on its

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.