为什么我不能将 List分配给到 IEnumerable
我尝试这样做:
IEnumerable<object> ids = new List<string>() { "0001", "0002", "0003" };
效果很好!
但是当我尝试这样做时:
IEnumerable<object> intIds = new List<System.Int32>() { 1, 2, 3 };
Visual Studio 告诉我: 无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.IEnumerable”。存在显式转换(您是否缺少强制转换?)
这是为什么?
I try to do this:
IEnumerable<object> ids = new List<string>() { "0001", "0002", "0003" };
it works great!
But when I try to do this:
IEnumerable<object> intIds = new List<System.Int32>() { 1, 2, 3 };
Visual Studio tells me:
Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)
Why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简而言之:.NET 4 中的通用方差不支持作为值类型的类型参数的方差。
它可以用于引用类型的原因是,一旦 CLR 确定它知道泛型转换是安全的,它就可以以相同的方式处理所有引用值 - 它们在内部具有相同的表示形式。将
string
引用转换为object
引用不需要实际转换,反之亦然,如果您知道它绝对是对字符串的引用 -基本上是相同的位。因此,生成的本机代码可以将引用视为引用,很高兴知道有关方差的规则保证了在类型安全级别不会发生任何令人讨厌的事情,并且不对值本身执行任何转换。对于值类型来说,情况并非如此,对于转换不是“引用转换”(即保留表示形式的转换)的引用类型来说,情况并非如此。这就是为什么你不能写
IEnumerable;顺便说一下,names = new List();
...Simply put: generic variance in .NET 4 doesn't support variance for type arguments which are value types.
The reason it can work for reference types is that once the CLR has decided it knows the generic conversion is safe, it can treat all reference values the same way - they have the same representation internally. No actual conversion is required to turn a
string
reference into anobject
reference, or vice versa if you know it's definitely a reference to a string - it's the same bits, basically. So the generated native code can just treat the references as references, happy in the knowledge that the rules around variance have guaranteed that nothing nasty will happen at the type safety level, and not performing any conversions on the values themselves.That isn't true for value types, or for reference types where the conversion isn't a "reference conversion" (i.e. a representation-preserving one). That's why you can't write
IEnumerable<XName> names = new List<string>();
by the way...来自 MSDN 博客 > C# 常见问题 >协方差和逆变常见问题解答:
From MSDN Blogs > C# Frequently Asked Questions > Covariance and Contravariance FAQ:
另一个解决方案是使用扩展方法 Enumerable.Cast ,如下所示:
Another solution would be to use the extension method Enumerable.Cast as such: