![](/img/trans.png)
[英]How to cast TElement of lookup; i.e. ILookup<TKey, Derived> to ILookup<TKey, Base>?
[英]Shouldn't ILookup<TKey, TElement> be (declared) covariant in TElement?
System.Linq.ILookUp定义读取
interface ILookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>, IEnumerable {
int Count {
get;
}
IEnumerable<TElement> this[TKey key] {
get;
}
bool Contains(TKey key);
}
由于IEnumerable在IGrouping <TKey,TElement>中是协变的,因此IGrouping <TKey,TElement>在TElement中是协变的,并且接口仅将TElement作为返回类型公开,我认为ILookup在TElement中也是协变的。 的确,定义
interface IMyLookup<TKey, out TElement> : IEnumerable<IGrouping<TKey, TElement>>, IEnumerable {
int Count {
get;
}
IEnumerable<TElement> this[TKey key] {
get;
}
bool Contains(TKey key);
}
编译没有问题。
那么,原始定义中缺少out关键字的原因可能是什么? 可能会添加Linq的未来版本吗?
跟踪MSDN文档,在.NET Framework 4中引入了泛型中的协方差和逆变,在此之前,从.NET Framework 2.0到.NET Framework 3.5,有IEnumerable<T>
。 然后在.NET Framework 4.0中,我们可以看到IEnumerable<out T>
其中类型参数T
为协方差。
从.NET Framework 3.5开始ILookup<TKey, TElement>
IGrouping<TKey, TElement>
和ILookup<TKey, TElement>
已经存在。 在.NET Framework 4.0中,前者已更新为IGrouping<out TKey, out TElement>
但后者在没有指定原因的情况下被省略。
TKey
不能协变,因为Contains(TKey)
和this[TKey]
阻止了这一点。
关于TElement
,问题尚不清楚。 我不相信设计师只是错过了它。 也许原因在于未来的计划。 或者他们想要阻止类似下面的内容,但我不知道为什么:
string[] strings = new[] {"a", "a", "b", "b", "b", "c"};
ILookup<string, string> lookup = strings.ToLookup(s => s); // Valid.
ILookup<string, object> lookup = strings.ToLookup(s => s); // Now invalid, but would correct if TElement was covariant (out TElement).
还有其他作者关注这个问题:
ToLookup :
值得注意的一点是,尽管IGrouping在TKey和TElement中是协变的,但ILookup在其两个类型参数中都是不变的。 虽然TKey必须是不变的,但TElement是协变的是合理的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.