[英]Why can't I return an IEnumerable<IGrouping<A,B>> as an IEnumerable<IEnumerable<B>>
I've used GroupBy()
, and produced an IEnumerable<IGrouping<TKey,TValue>>
. 我使用了
GroupBy()
,并生成了IEnumerable<IGrouping<TKey,TValue>>
。
And I can foreach over it, and pass each element (each IGrouping<TKey,TValue>
) into a method that accepts IEnumerable<TValue>
我可以遍历它,并将每个元素(每个
IGrouping<TKey,TValue>
)传递到接受IEnumerable<TValue>
This is unsurprising - IGrouping<TKey,TValue>
implements IEnumerable<TValue>
这不足为奇
IGrouping<TKey,TValue>
实现IEnumerable<TValue>
But if I define a method that accepts IEnumerable<IEnumerable<B>>
, then I can't directly pass in the output of GroupBy()
, even though it is an IEnumerable<>
and every element inside it is an IEnumerable<B>
但是,如果我定义了一个接受
IEnumerable<IEnumerable<B>>
,那么即使它是IEnumerable<>
并且其中的每个元素都是IEnumerable<B>
,我也无法直接传递GroupBy()
的输出。
Why? 为什么?
正如几个人所指出的(确实,正如我在脑海中所知道的那样),这仅仅是一个协方差问题,并且仅在.NET 4之前的环境中才出现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.