我想知道为什么以下代码无法编译,除了"实例参数:无法从'System.Collections.Generic.IEnumerable
public static List<TInterface> Foo<TInterface, TImpl>(IEnumerable<TImpl> input)
where TImpl : TInterface
{
return input.ToList<TInterface>();
}
我知道我可以将返回行改为input.Cast<TInterface>().ToList()
,但是想要理解为什么所写的代码不能编译。在我看来,编译器似乎应该能够验证输入可以隐式转换为IEnumerable
方差只适用于类。
添加class,
到你的约束。