c# – 为什么我不能将List>传递给IEnumerable?

代码生成两个编译时错误
private void DoSomething()
{
    List<List<Foo>> myFoos = GetFoos();

    UseFoos(myFoos);
}

private void UseFoos(IEnumerable<IEnumerable<Foo>>)
{

}

‘NameSpace.Class.UseFoos(System.Collections.Generic.IEnumerable< System.Collections.Generic.IEnumerable< Foo>)’的最佳重载方法匹配有一些无效参数

参数1:无法从“System.Collections.Generic.List< System.Collections.Generic.List&Foo>”中转换到’System.Collections.Generic.IEnumerable< System.Collections.Generic.IEnumerable< Foo>>’

投射到IEnumberable< List< Foo>>不是问题有什么不同于投射失败的类型的内部List组件?

解决方法

编辑:我刚刚意识到,我没有真正回答如何解决这个限制的方面.幸运的是很简单:
UseFoos(myFoos.Cast<IEnumerable<Foo>>());

代码编译正确(当您给予UseFoos参数一个名称)在C#4下,它介绍了通用协方差和接口和委托的逆变.

作为一个更简单的例子,这个工作在C#4中但不是C#3中:

IEnumerable<string> strings = new List<string>();
IEnumerable<object> objects = strings;

请注意,即使在C#4中,类不是不变的,所以这将不起作用:

// This won't work
List<string> strings = new List<string>();
List<object> objects = strings;

…甚至对于接口,只有在安全的情况下才支持它:

// This won't work either
IList<string> strings = new List<string>();
IList<object> objects = strings;

接口(或委托)必须声明类型参数本身的方差,所以如果你看看.NET 4 documentation for IEnumerable<T>,你会看到它被声明为

public interface IEnumerable<out T>

在哪里声明了T中的协方差.

Eric Lippert在covariance and contravariance博客类别中有更多关于这一点.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...