加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 18:00:06 所属栏目:百科 来源:网络整理
导读:此代码生成两个编译时错误: private void DoSomething(){ ListListFoo myFoos = GetFoos(); UseFoos(myFoos);}private void UseFoos(IEnumerableIEnumerableFoo){} ‘NameSpace.Class.UseFoos(System.Collections.Generic.IEnumerable System.Collections.G
此代码生成两个编译时错误:
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的博客类别中有更多关于这一点.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读