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

  • Home
  • SEARCH
  • 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 8709807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:27:39+00:00 2026-06-13T04:27:39+00:00

Since IEnumerable has a covariant parameter in C# 4.0 I am confused how it

  • 0

Since IEnumerable has a covariant parameter in C# 4.0 I am confused how it is behaving in the following code.

public class Test
{
    IEnumerable<IFoo> foos;

    public void DoTestOne<H>(IEnumerable<H> bars) where H : IFoo
    {
        foos = bars;
    }

    public void DoTestTwo(IEnumerable<IBar> bars)
    {
        foos = bars;
    }
}
public interface IFoo
{
}
public interface IBar : IFoo
{
}

So basically the DoTestOne method doesn’t compile while DoTestTwo does. In addition to why it doesn’t work, if anyone knows how I can achieve the effect of DoTestOne (assigning an IEnumberable<H> where H : IFoo to an IEnumberable<IFoo>) I would appreciate the help.

  • 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-13T04:27:40+00:00Added an answer on June 13, 2026 at 4:27 am

    If you know that H will be a class, this does work:

        public void DoTestOne<H>(IEnumerable<H> bars) where H : class, IFoo
        {
            foos = bars;
        }
    

    The issue here is that if H is a value type, the covariance is not exactly what you’d expect, as IEnumerable<MyStruct> actually returns the value types whereas IEnumerable<IFoo> has to return boxed instances. You can use an explicit Cast<IFoo> to get around this, if necessary.

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

Sidebar

Related Questions

Noob question: Consider the following C# code: public IEnumerable<xpto> CalculatedList { get { foreach(var
Let's take the interface IQueryable<T> for example: public interface IQueryable<T> : IQueryable, IEnumerable; Since
Good morning, I wrote the following LINQ query public static Func<String, IEnumerable<String>> ListaCorreccoes =
I have this IEnumerable of type Question.. the class Question has the properties, Question(string)
I have the following 2 entities: public class Product { [Key] public int ID
Since REST is stateless, each request that comes in has no knowledge of the
Since each thread has its own stack, its private data can be put on
I'm getting a compile-time error that my method call has some invalid arguments. public
So, I have a query like public static IEnumerable<Archive> GetArchivesRecursive(this ISession session, Page rootPage)
If I have a method that requires a parameter that, Has a Count property

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.