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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:26:08+00:00 2026-05-27T16:26:08+00:00

This code generates two compile time errors: private void DoSomething() { List<List<Foo>> myFoos =

  • 0

This code generates two compile time errors:

private void DoSomething()
{
    List<List<Foo>> myFoos = GetFoos();

    UseFoos(myFoos);
}

private void UseFoos(IEnumerable<IEnumerable<Foo>>)
{

}

The best overloaded method match for 'NameSpace.Class.UseFoos(System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<Foo>>)' has some invalid arguments

and

Argument 1: cannot convert from 'System.Collections.Generic.List<System.Collections.Generic.List<Foo>>' to 'System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<Foo>>'

Casting to IEnumberable<List<Foo>> isn’t a problem. What’s different about casting the inner List component of the type that it fails?

  • 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-27T16:26:09+00:00Added an answer on May 27, 2026 at 4:26 pm

    EDIT: I’ve just realized that I haven’t really answered the aspect of how to work around the limitation. Fortunately it’s quite easy:

    UseFoos(myFoos.Cast<IEnumerable<Foo>>());
    

    That code compiles fine (when you’ve given the UseFoos parameter a name) under C# 4, which introduced generic covariance and contravariance for interfaces and delegates.

    As a simpler example, this works in C# 4 but not in C# 3:

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

    Note that even in C# 4, classes aren’t invariant, so this won’t work:

    // This won't work
    List<string> strings = new List<string>();
    List<object> objects = strings;
    

    … and even for interfaces, it’s only supported when it’s safe:

    // This won't work either
    IList<string> strings = new List<string>();
    IList<object> objects = strings;
    

    The interface (or delegate) has to declare the variance of the type parameter itself, so if you look at the .NET 4 documentation for IEnumerable<T> you’ll see it’s declared as

    public interface IEnumerable<out T>
    

    where out declares the covariance in T.

    Eric Lippert has a lot more about this in his blog category of covariance and contravariance.

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

Sidebar

Related Questions

I have some LINQ code that generates a list of strings, like this: var
Ok, this situation is a little weird but anyway. This PHP code generates several
I have some C# code that generates google maps. This codes looks at all
I'm trying to compile my project from an external application that generates two versions
I use this code to generate a random number. Random R = new Random(0);
This issue is somewhat related: Problem with Code Generated by XSD.EXE: Sequence of Elements
I have this code try { //AN EXCEPTION IS GENERATED HERE!!! } catch {
I have this php code, with which I am trying to generate a popup
This is the code that I'm using to generate a DH keypair: KeyPairGenerator keyGen
In a View, code like this will generate the right URL to jump to

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.