由于IEnumerable在 C# 4.0 中有一个协变参数,我对它在以下代码中的行为感到困惑。
public class Test
{
IEnumerable<IFoo> foos;
public void DoTestOne<H>(IEnumerable<H> bars) where H : IFoo
{
foos = bars;
}
public void DoTestTwo(IEnumerable<IBar> bars)
{
foos = bars;
}
}
public interface IFoo
{
}
public interface IBar : IFoo
{
}
所以基本上该DoTestOne方法在编译时不会编译DoTestTwo。除了为什么它不起作用之外,如果有人知道我如何实现DoTestOne(将 an 分配IEnumberable<H> where H : IFoo给 an IEnumberable<IFoo>)的效果,我将不胜感激。