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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:27:59+00:00 2026-05-15T04:27:59+00:00

The following code fails to compile (using VS2010) and I don’t see why. The

  • 0

The following code fails to compile (using VS2010) and I don’t see why. The compiler should be able to infer that List<TestClass> is ‘compatible’ (sorry for lack of a better word) with IEnumerable<ITest>, but somehow it doesn’t. What am I missing here?


interface ITest {
    void Test();
}


class TestClass : ITest {
    public void Test() {
    }
}

class Program {
    static void Test(IEnumerable<ITest> tests) {
        foreach(var t in tests) {
            Console.WriteLine(t);
        }
    }
    static void Main(string[] args) {
        var lst = new List<TestClass>();

        Test(lst); // fails, why?

        Test(lst.Select(t=>t as ITest)); //success

        Test(lst.ToArray()); // success
    }
}

The compiler gives two errors:

  1. The best overloaded method match for ‘ConsoleApplication1.Program.Test(System.Collections.Generic.IEnumerable<ConsoleApplication2.ITest>)’ has some invalid arguments

  2. Argument 1: cannot convert from ‘System.Collections.Generic.List<ConsoleApplication2.TestClass>’ to ‘System.Collections.Generic.IEnumerable<ConsoleApplication2.ITest>’

  • 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-15T04:28:00+00:00Added an answer on May 15, 2026 at 4:28 am

    What you are trying to do is called covariance – converting from a narrower type (TestClass) to a wider type (ITest). It’s something you’ll be used to all the time, it happens when you convert from a float to a double for example.

    Unfortunately .Net 3.5 and lower does not support covariance in generic classes.

    .Net 4.0 now does support covariance (and contravariance) in generics, provided those generic classes are compiled with the keywords out for covariant types and in for contravarient types. IEnumerable in .Net 4.0 is defined as covariant. If you right click on the IEnumerable type and click “goto definition”, you’ll see this:

    public interface IEnumerable<out T> : IEnumerable
    

    If you are using VS2010, you will need to make sure your project is targeting .net 4.0. This can be changed from the project properties. Right click on the project, select properties, goto the “Application” tab and check that the “Target framework” is to to .Net 4.

    MSDN has more information.

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

Sidebar

Related Questions

The following code fails to compile #include <iostream> #include <cmath> #include <complex> using namespace
I have the following code that fails when i run it. my .h file:
This code fails to compile using g++ 4.2.1 but works fine under vc++ v8.
The following code fails to compile on g++ 4.6.1: template<class Base> struct GetBase {
Why does the following code compile for the line using a non-const iterator but
The code below fails to compile with g++ version 4.5.0 using the -std=c++0x switch.
I m trying to compile this code and linking fails with the following error:
I have the following code and it fails to compile template < typename T
The following code compiles in Visual C++ and gcc, but fails with Code Warrior
The following code fails in 'Evaluate' with: This expression was expected to have type

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.