微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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博客类别中有更多关于这一点.

原文地址:https://www.jb51.cc/csharp/97211.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐