Shouldn't ILookup be (declared) covariant in TElement?

前端 未结 1 1758
广开言路
广开言路 2021-02-12 14:43

The definition System.Linq.ILookUp reads

interface ILookup : IEnumerable>, IEnumerable {
    int Coun         


        
相关标签:
1条回答
  • 2021-02-12 15:12

    Tracking the MSDN documentation, Covariance and Contravariance in Generics have been introduced in .NET Framework 4 Prior to that, there was IEnumerable<T> since the .NET Framework 2.0 up to .NET Framework 3.5. Then in .NET Framework 4.0 we can see IEnumerable<out T> with type parameter T as covariance.

    IGrouping<TKey, TElement> and ILookup<TKey, TElement> have existed since .NET Framework 3.5. In .NET Framework 4.0 the former has been updated to IGrouping<out TKey, out TElement> but the latter has been omitted without specifying the reason.

    TKey can't be covariant since implementations of Contains(TKey) and this[TKey] prevent that.

    With regard to TElement the issue is not clear. I don't believe that designers just missed it. Perhaps cause lies in the plans for the future. Or they wanted to prevent something like the below, but I don't know why:

    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).
    

    There are also other authors, that pay attention to that issue:

    ToLookup:

    One slightly odd point to note is that while IGrouping is covariant in TKey and TElement, ILookup is invariant in both of its type parameters. While TKey has to be invariant, it would be reasonable for TElement to be covariant

    0 讨论(0)
提交回复
热议问题