big*_*gge 18 .net c# linq generics covariance
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);
}
Run Code Online (Sandbox Code Playgroud)
由于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);
}
Run Code Online (Sandbox Code Playgroud)
编译没有问题.
那么,原始定义中缺少out关键字的原因可能是什么?可能会添加Linq的未来版本吗?
跟踪MSDN文档,在.NET Framework 4中引入了泛型中的协方差和逆变,在此之前,IEnumerable<T>
从.NET Framework 2.0到.NET Framework 3.5.然后在.NET Framework 4.0中我们可以看到IEnumerable<out T>
类型参数T
作为协方差.
IGrouping<TKey, TElement>
并且ILookup<TKey, TElement>
自.NET Framework 3.5以来就已存在.在.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).
Run Code Online (Sandbox Code Playgroud)
还有其他作者关注这个问题:
值得注意的一点是,尽管IGrouping在TKey和TElement中是协变的,但ILookup在其两个类型参数中都是不变的.虽然TKey必须是不变的,但TElement是协变的是合理的