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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:21:19+00:00 2026-06-11T11:21:19+00:00

IEnumerable<T> is co-variant but it does not support value type, just only reference type.

  • 0

IEnumerable<T> is co-variant but it does not support value type, just only reference type. The below simple code is compiled successfully:

IEnumerable<string> strList = new List<string>();
IEnumerable<object> objList = strList;

But changing from string to int will get compiled error:

IEnumerable<int> intList = new List<int>();
IEnumerable<object> objList = intList;

The reason is explained in MSDN:

Variance applies only to reference types; if you specify a value type for a variant type parameter, that type parameter is invariant for the resulting constructed type.

I have searched and found that some questions mentioned the reason is boxing between value type and reference type. But it does not still clear up my mind much why boxing is the reason?

Could someone please give a simple and detailed explanation why covariance and contravariance do not support value type and how boxing affects this?

  • 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-11T11:21:20+00:00Added an answer on June 11, 2026 at 11:21 am

    Basically, variance applies when the CLR can ensure that it doesn’t need to make any representational change to the values. References all look the same – so you can use an IEnumerable<string> as an IEnumerable<object> without any change in representation; the native code itself doesn’t need to know what you’re doing with the values at all, so long as the infrastructure has guaranteed that it will definitely be valid.

    For value types, that doesn’t work – to treat an IEnumerable<int> as an IEnumerable<object>, the code using the sequence would have to know whether to perform a boxing conversion or not.

    You might want to read Eric Lippert’s blog post on representation and identity for more on this topic in general.

    EDIT: Having reread Eric’s blog post myself, it’s at least as much about identity as representation, although the two are linked. In particular:

    This is why covariant and contravariant conversions of interface and delegate types require that all varying type arguments be of reference types. To ensure that a variant reference conversion is always identity-preserving, all of the conversions involving type arguments must also be identity-preserving. The easiest way to ensure that all the non-trivial conversions on type arguments are identity-preserving is to restrict them to be reference conversions.

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

Sidebar

Related Questions

Since IEnumerable.Contains() method does not accept a predicate as an argument, Most people use
IEnumerable does not guarantee that enumerating twice will yield the same result. In fact,
Does an IEnumerable have to use Yield to be deferred? Here is test code
IEnumerable<T> , IComparable<T> and a few more are now type-variant. IList<T> , ICollection<T> and
delegate IEnumerable<T> GetFromSQLDelegate<T>(...); public GetFromSQLDelegate myFunctionToCall; The above does not compile because myFunctionToCall does
I have a variant for initializing DbRecordData public class DbRecordData : IList<DbFieldValue>, ICollection<DbFieldValue>, IEnumerable<DbFieldValue>,
If I have an IEnumerable object in my .net code that contains a single
i have this code : IEnumerable<string> q = customers /*EF entity*/ .Select (c =>
ExecuteQuery() method returns an IEnumerable but is there a way to make it return
This post ( http://blogs.msdn.com/b/brada/archive/2005/01/18/355755.aspx ) says IEnumerable<T> is Contra-variant. However type T is co-variant

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.