IEnumerable 是否总是意味着集合?
关于 IEnumerable
只是一个简单的问题:
IEnumerable
是否始终意味着集合?或者在单个对象上使用它是否合法/可行/可以/什么?
Just a quick question regarding IEnumerable
:
Does IEnumerable
always imply a collection? Or is it legitimate/viable/okay/whatever to use on a single object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
IEnumerable
和IEnumerable
接口建议某种序列,但该序列不必是具体的集合。例如,在这种情况下,底层的具体集合在哪里?
The
IEnumerable
andIEnumerable<T>
interfaces suggest a sequence of some kind, but that sequence doesn't need to be a concrete collection.For example, where's the underlying concrete collection in this case?
IEnumerable
始终强制用于单个对象 - 该单个对象始终是零个或多个其他对象的持有者或生产者不一定与IEnumerable
有任何关系。IEnumerable
表示一个集合,这是常见的,但不是强制性的。枚举可以是集合,也可以是生成器、查询,甚至是计算。
生成器:
查询:
计算:
LINQ 本身就是一系列运算符,它们生成实现不具有任何底层集合的
IEnumerable
的对象。顺便说一句,好问题!
注意:任何对
IEnumerable
的引用也适用于IEnumerable
,因为后者继承了前者。It is always and mandatory that
IEnumerable
is used on a single object - the single object is always the holder or producer of zero or more other objects that do not necessarily have any relation toIEnumerable
.It's usual, but not mandatory, that
IEnumerable
represents a collection.Enumerables can be collections, as well as generators, queries, and even computations.
Generator:
Query:
Computation:
LINQ is itself a series of operators that produce objects that implement
IEnumerable<T>
that don't have any underlying collections.Good question, BTW!
NB: Any reference to
IEnumerable
also applies toIEnumerable<T>
as the latter inherits the former.是的,
IEnumerable
意味着项目的集合或可能的集合。该名称源自 enumerate,意思是:
Yes,
IEnumerable
implies a collection, or possible collection, of items.The name is derived from enumerate, which means to:
根据文档,它通过集合公开枚举器。
According to the docs, it exposes the enumerator over a collection.
您当然可以在单个对象上使用它,但是该对象将被公开为包含单个对象的枚举,即您可以拥有一个
IEnumerable
单个整数:You can certainly use it on a single object, but this object will then just be exposed as an enumeration containing a single object, i.e. you could have an
IEnumerable<int>
with a single integer:IEnumerable
表示可以枚举的集合,而不是单个项目。查看 MSDN;该接口公开了GetEnumerator()
,其中IEnumerable
represents a collection that can be enumerated, not a single item. Look at MSDN; the interface exposesGetEnumerator()
, which是的,IEnumerable 总是意味着集合,这就是枚举的意思。
单个对象的用例是什么?
我不认为在单个对象上使用它有问题,但为什么要这样做呢?
Yes, IEnumerable always implies a collection, that is what enumerate means.
What is your use case for a single object?
I don't see a problem with using it on a single object, but why do want to do this?
我不确定你的意思是“集合”还是.NET“ICollection”,但由于其他人只提到了前者,我将提到后者。
http://msdn.microsoft.com/en-us/library/92t2ye13.aspx
根据该定义,所有 ICollection 都是 IEnumerable。但反之则不然。
但大多数数据结构(甚至数组)只是实现这两个接口。
按照这个思路:您可以有一个不公开内部数据结构的汽车站(单个对象),并将 IEnumerable 放在上面。我想。
I'm not sure whether you mean a "collection" or a .NET "ICollection" but since other people have only mentioned the former I will mention the latter.
http://msdn.microsoft.com/en-us/library/92t2ye13.aspx
By that definition, All ICollections are IEnumerable. But not the other way around.
But most data structure (Array even) just implement both interfaces.
Going on this train of thought: you could have a car depot (a single object) that does not expose an internal data structure, and put IEnumerable on it. I suppose.