I wonder what point I overlook. IGrouping<out TKey, out TElement>
is used by LINQ GroupBy
. Further, the same logic is applied to ILookup
, IIRC. Accordingly, they can return multiple values subject to the key specified. However, instead, I would expect the following signature to be done so.
interface IGrouping<out TKey, out IEnumerable<TElement>>
^^^^^^^^^^^^^
Originally, it implements IEnumerable<TElement>
and IEnumerable
for backward compatibility. Does this accomplish the same task? If yes, how? I don't understand.
public interface ILookup<TKey,TElement> : IEnumerable<IGrouping<TKey,TElement>>
Does the followed mean ILookup
used by Join/GroupJoin
has multiple keys with the association of multiple values? Could you exemplify? I've looked the question, but don't understand the difference, except that their lazy evaluation vs immediate evaluation semantics.
Not to do complicate matter further, but the output may help for everyone.
If you look at the declaration of
IGrouping
,You can see that it says "
IGrouping<TKey, TElement>
is a kind ofIEnumerable<TElement>
".It's a special kind of
IEnumerable<TElement>
. Special in what way? It has aKey
associated.And no, this is not valid C# syntax:
Remember that this is an interface declaration, and in the
<>
, it is declaring the type parameters, which are identifiers, for the interface.IEnumerable<TElement>
is not a valid identifier. You also seem to want to refer to the existing interfaceSystem.Collections.Generic.IEnumerable<T>
, but it makes no sense to put it in this place in the declaration, the same way that it wouldn't make sense to writeinterface Foo<List<T>>
as an interface declaration in Java.If you mean:
that could work, and there are many ways in which you can design this
IGrouping
interface. The following way probably makes sense to you the most?But what's really great about .NET framework's design is that it implements an existing interface, and that allows it to be used as an
IEnumerable<TElement>
directly, which is a very powerful thing. If your class have the ability that the interface promises, implement that interface. See also.Yes. Since
ILookup
implementsIEnumerable<IGrouping>
, andIGrouping
implementsIEnumerable
,ILookup
is kind of like aIEnumerable<IEnumerable>
(forgive me using the generics notation very loosely here). So anILookup
an enumerable of enumerables, i.e. like a 2D array. The difference is, that each inner "array" has aKey
, since the inner "arrays" are actuallyIGrouping
s.