Reputation: 4677
I am wondering why the following code fails to compile, with the exception "Instance argument: cannot convert from 'System.Collections.Generic.IEnumerable<TImpl>' to 'System.Collections.Generic.IEnumerable<TInterface>'":
public static List<TInterface> Foo<TInterface, TImpl>(IEnumerable<TImpl> input)
where TImpl : TInterface
{
return input.ToList<TInterface>();
}
I know that I could change the return line to be input.Cast<TInterface>().ToList()
instead, but want to understand why the code as written doesn't compile. In my mind, it seems as though the compiler should be able to verify that input can be implicitly cast to IEnumerable<TInterface>.
Upvotes: 2
Views: 38