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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:06:54+00:00 2026-05-15T17:06:54+00:00

I have the following class and extension class (for this example): public class Person<T>

  • 0

I have the following class and extension class (for this example):

public class Person<T>
{
    public T Value { get; set; }
}

public static class PersonExt
{
    public static void Process<TResult>(this Person<IEnumerable<TResult>> p)
    {
        // Do something with .Any().
        Console.WriteLine(p.Value.Any());
    }
}

I was expecting I could write something like the following and it would work, but it doesn’t:

var x = new Person<List<String>>();
x.Process();

Since List is lower in the inheritance tree than IEnumerable, shouldn’t this be valid? It works if I new up a Person<IEnumerable<String>> of course because that’s the direct type.

I’m trying to use an extension method that can be applied to all Person<T>‘s as long as T implements IEnumerable<Something> because I need to use the .Any() method.

EDIT: Maybe my understanding of covariance is off? I know IEnumerable<String> should convert to IEnumerable<Object>, but couldn’t IList<String> convert to IEnumerable<String>?

EDIT2: Forgot to mention that I am using .net 4.0.

  • 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-15T17:06:54+00:00Added an answer on May 15, 2026 at 5:06 pm

    I know IEnumerable<String> should
    convert to IEnumerable<Object>, but
    couldn’t IList<String> convert to
    IEnumerable<String>?

    IList<String> can convert to IEnumerable<String>. The problem is that you’re trying to convert Person<List<String>> to Person<IEnumerable<String>>, which is illegal. For example, it’s perfectly valid to write:

    var x = new Person<IEnumerable<String>>();
    x.Value = new string[0];
    

    since Value is of type IEnumerable<String> and a string array is an IEnumerable<String>. However, you cannot write:

    var x = new Person<List<String>>();
    x.Value = new string[0];
    

    since Value is of type List<String>. Since you can’t use a Person<List<String>> in all places where you could use a Person<IEnumerable<String>>, it’s not a legal cast.

    Note that you can do something similar to what you want if you add a second type parameter to your extension method:

    public static void Process<TResult, TList>(this Person<TList> p)
        where TList : IEnumerable<TResult>
    {
        Console.WriteLine(p.Value.Any());
    }
    

    Unfortunately, the compiler won’t be able to infer both type parameters, so you would have to call it like this:

    var x = new Person<List<String>>();
    x.Process<String, List<String>>();
    

    If you are using C# 4.0 and can use covariance, then you can define a covariant interface for person:

    public interface IPerson<out T>
    {
        T Value { get; }
    }
    
    public class Person<T>
        : IPerson<T>
    {
        public T Value { get; set; }
    }
    

    And then write your extension method as:

    public static void Process<TResult>(this IPerson<IEnumerable<TResult>> p)
    {
        // Do something with .Any().
        Console.WriteLine(p.Value.Any());
    }
    

    Since IPerson<T>.Value is read-only, a IPerson<List<String>> can be used everywhere that an IPerson<IEnumerable<String>> can be, and the conversion is valid.

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

Sidebar

Related Questions

I have the following extension method: public static void ThrowIfArgumentIsNull<T>(this T value, string argument)
This extension method does not work on two separate development machines: public static string
Given the following two interfaces (these are small examples, not my actual implementation): public
So I want to be able to add/remove class methods at runtime. Before you
I'm going to develop a Firefox extension which uses some Java classes. The extension
I have an object that needs to be serialized to an EDI format. For
I hope the title and following text are clear, I'm not very familiar with
I've been creating a small application that allows a user to convert images to
How do you lazy load nested lists without actually executing the query? Using a

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.