Search code examples
c#anonymous-types

Why doesn't IEnumerable of anonymous types return a List<object> on ToList()?


Here is a simplified function that I want to create:

static List<object> GetAnonList(IEnumerable<string> names)
{
    return names.Select(name => new { FirstName = name }).ToList();
}

In that code block, I get the compiler error:

Error CS0029 Cannot implicitly convert type 'System.Collections.Generic.List<>' to 'System.Collections.Generic.List'

In the documentation for Anonymous types, it says that anonymous types are treated as type object. Why doesn't the C# compiler return a List<object> on names.ToList()?

Further more, why doesn't the following code cause an error? If List<<anonymous type: string FirstName>> cannot be converted to List<object>, then why can it be converted to IEnumerable<object>?

static IEnumerable<object> GetAnonList(IEnumerable<string> names)
{
    return names.Select(name => new { FirstName = name }).ToList();
}

Solution

  • If List<<anonymous type: string FirstName>> cannot be converted to List<object>, then why can it be converted to IEnumerable<object>?

    That's because IEnumerable<T> is covariant and List<T> is not. It has nothing to do with anonymous types.

    If the code you wrote was to work you'd be able to use List<string> as List<object> and add anything to it breaking type safety.

    You can make your code work by passing a generic type parameter to ToList call:

    static List<object> GetAnonList(IEnumerable<string> names)
    {
        return names.Select(name => new { FirstName = name }).ToList<object>();
    }
    

    But there is very little you can do with that outside of this method. You won't be able to access FirstName property unless you use reflection.