[英]How can interface IGrouping<out TKey, out TElement> yield multiple values?
I wonder what point I overlook.我想知道我忽略了哪一点。
IGrouping<out TKey, out TElement>
is used by LINQ GroupBy
. IGrouping<out TKey, out TElement>
被 LINQ GroupBy
使用。 Further, the same logic is applied to ILookup
, IIRC.此外,相同的逻辑适用于
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.最初,它实现了
IEnumerable<TElement>
和IEnumerable
以实现向后兼容性。 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?以下是否意味着
Join/GroupJoin
ILookup
使用的 ILookup 具有多个键与多个值的关联? 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.不要让事情变得更复杂,但 output 可能对每个人都有帮助。
If you look at the declaration of IGrouping
,如果您查看
IGrouping
的声明,
public interface IGrouping<out TKey,out TElement> : System.Collections.Generic.IEnumerable<out TElement>
You can see that it says " IGrouping<TKey, TElement>
is a kind of IEnumerable<TElement>
".你可以看到它说“
IGrouping<TKey, TElement>
is a kind of IEnumerable<TElement>
”。
It's a special kind of IEnumerable<TElement>
.这是一种特殊的
IEnumerable<TElement>
。 Special in what way?特别在什么方面? It has a
Key
associated.它有一个关联的
Key
。
And no, this is not valid C# syntax:不,这不是有效的 C# 语法:
interface IGrouping<out TKey, out IEnumerable<TElement>>
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. IEnumerable<TElement>
不是有效标识符。 You also seem to want to refer to the existing interface System.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 write interface Foo<List<T>>
as an interface declaration in Java.您似乎还想引用现有的接口
System.Collections.Generic.IEnumerable<T>
,但是将其放在声明中的这个位置是没有意义的,就像编写interface Foo<List<T>>
没有意义一样interface Foo<List<T>>
作为 Java 中的接口声明。
If you mean:如果你的意思是:
interface IGrouping<out TKey, out TElement> where TElement : IEnumerable
that could work, and there are many ways in which you can design this IGrouping
interface.这可以工作,并且有很多方法可以设计这个
IGrouping
接口。 The following way probably makes sense to you the most?以下方式可能对您最有意义?
interface IMyGrouping<TKey, TElement> {
TKey Key { get; }
IEnumerable<TElement> Elements { get; }
}
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.但是.NET框架的设计真正厉害的地方在于它实现了一个已有的接口,并且可以直接作为一个
IEnumerable<TElement>
使用,这是一个非常强大的东西。 If your class have the ability that the interface promises, implement that interface.如果您的 class 具有接口承诺的能力,请实现该接口。 See also .
另请参阅。
Does the followed mean ILookup used by GroupBy has multiple keys with the association of multiple values?
以下是否意味着 GroupBy 使用的 ILookup 具有多个键与多个值的关联?
Yes.是的。 Since
ILookup
implements IEnumerable<IGrouping>
, and IGrouping
implements IEnumerable
, ILookup
is kind of like a IEnumerable<IEnumerable>
(forgive me using the generics notation very loosely here).由于
ILookup
实现了IEnumerable<IGrouping>
,而IGrouping
实现了IEnumerable
, ILookup
有点像IEnumerable<IEnumerable>
(请原谅我在这里非常松散地使用了 generics 表示法)。 So an ILookup
an enumerable of enumerables, ie like a 2D array.因此,
ILookup
是一个可枚举的可枚举,即像一个二维数组。 The difference is, that each inner "array" has a Key
, since the inner "arrays" are actually IGrouping
s.不同之处在于,每个内部“数组”都有一个
Key
,因为内部“数组”实际上是IGrouping
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.