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 public interface IEnumerable<out T> 在哪里声明了T中的协方差. Eric Lippert在covariance and contravariance的博客类别中有更多关于这一点. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- c# – Kestrel在https与asp.net core 1.0
- C#中datagridview使用tooltip控件显示单元格内容的方法
- oracle 11g r2 一键配置脚本
- ruby-on-rails-3 – 来自辅助方法的意外点
- 如何在不询问iOS Swift登录的情况下自动登录?
- osx下配置Cocos2d-x 3.x Android开发环境
- iphone – AGVTool新版和什么版本不对应
- objective-c – 使用iOS 8的iCloud,Core Data和Xcode 6.01的
- Swift:替换string中的字符 – 没有stringByReplacingOccur
- reactjs – React 0.13 this.getDOMNode()等价于React.find