简体   繁体   中英

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 IEnumberable<object> ?

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

If List<<anonymous type: string FirstName>> cannot be converted to List<object> , then why can it be converted to IEnumberable<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.

IEnumerable<out T> IEnumerable(T) Document

List<T> List(T) Document

Note that there is "out" Generic Modifier on IEnumerable interface, which allows you put more derived type to IEnumerable.

See out (Generic Modifier) (C# Reference)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM