应该';t I查找<;TKey、TElement>;在TElement中是(声明的)协变的

本文关键字:TElement 中是 声明 查找 lt 应该 TKey gt | 更新日期: 2023-09-27 17:59:29

定义System.Linq.ILookUp<TKey, TElement>读取

interface ILookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>, IEnumerable 
{
    int Count { get; }
    IEnumerable<TElement> this[TKey key] { get; }
    bool Contains(TKey key);
}

由于IEnumerableIGrouping<TKey, TElement>中是协变的,IGrouping<TKey, TElement>TElement中是协变量的,并且接口仅将TElement公开为返回类型,因此我假设ILookupTElement中也是协变的。事实上,的定义

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的未来版本中吗?

应该';t I查找<;TKey、TElement>;在TElement中是(声明的)协变的

跟踪MSDN文档,Generic中的协方差和方差已在.NET Framework 4中引入。在此之前,从.NET Framework 2.0到.NET Framework 3.5都有IEnumerable<T>。然后在.NET Framework 4.0中,我们可以看到类型参数T作为协方差的IEnumerable<out T>

CCD_ 12和CCD_。在.NET Framework 4.0中,前者已更新为IGrouping<out TKey, out TElement>,但后者已被省略,但未说明原因。

CCD_ 15不能是协变的,因为CCD_ 16和CCD_。

关于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是协变是合理的