C# IEnumerator 和 IEnumerable 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/619564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between IEnumerator and IEnumerable?
提问by
Possible Duplicate:
Can anyone explain IEnumerable and IEnumerator to me?
What are the differences between IEnumerator and IEnumerable?
IEnumerator 和 IEnumerable 有什么区别?
采纳答案by cgreeno
IEnumerableis an interface that defines one method GetEnumeratorwhich returns an IEnumeratorinterface, this in turn allows readonly access to a collection. A collection that implements IEnumerable can be used with a foreach statement.
IEnumerable是一个接口,它定义了一个方法GetEnumerator,它返回一个IEnumerator接口,这反过来又允许对集合进行只读访问。实现 IEnumerable 的集合可以与 foreach 语句一起使用。
Definition
定义
IEnumerable
public IEnumerator GetEnumerator();
IEnumerator
public object Current;
public void Reset();
public bool MoveNext();
回答by James Kingsbery
IEnumerable
and IEnumerator
are both interfaces. IEnumerable
has just one method called GetEnumerator
. This method returns (as all methods return something including void) another type which is an interface and that interface is IEnumerator
. When you implement enumerator logic in any of your collection class, you implement IEnumerable
(either generic or non generic). IEnumerable
has just one method whereas IEnumerator
has 2 methods (MoveNext
and Reset
) and a property Current
. For easy understanding consider IEnumerable
as a box that contains IEnumerator
inside it (though not through inheritance or containment). See the code for better understanding:
IEnumerable
并且IEnumerator
都是接口。IEnumerable
只有一种方法叫做GetEnumerator
. 这个方法返回(因为所有方法都返回一些东西,包括 void)另一种类型,它是一个接口,该接口是IEnumerator
. 当您在任何集合类中实现枚举器逻辑时,您将实现IEnumerable
(泛型或非泛型)。IEnumerable
只有一个方法,而IEnumerator
有 2 个方法(MoveNext
和Reset
)和一个属性Current
。为便于理解,将其IEnumerable
视为一个包含IEnumerator
在其中的盒子(尽管不是通过继承或包含)。请参阅代码以更好地理解:
class Test : IEnumerable, IEnumerator
{
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
public object Current
{
get { throw new NotImplementedException(); }
}
public bool MoveNext()
{
throw new NotImplementedException();
}
public void Reset()
{
throw new NotImplementedException();
}
}
回答by Ryan Lundy
An IEnumerator
is a thing that can enumerate: it has the Current
property and the MoveNext
and Reset
methods (which in .NET code you probably won't call explicitly, though you could).
一个IEnumerator
是可以列举一件事:它有Current
属性和MoveNext
与Reset
方法(在.NET代码,你可能不显式调用,尽管你可以)。
An IEnumerable
is a thing that can be enumerated...which simply means that it has a GetEnumerator method that returns an IEnumerator
.
AnIEnumerable
是一个可以枚举的东西……这只是意味着它有一个 GetEnumerator 方法,它返回一个IEnumerator
.
Which do you use? The only reason to use IEnumerator
is if you have something that has a nonstandard way of enumerating (that is, of returning its various elements one-by-one), and you need to define how that works. You'd create a new class implementing IEnumerator
. But you'd still need to return that IEnumerator
in an IEnumerable
class.
你用哪个?使用的唯一原因IEnumerator
是,如果您有一些非标准的枚举方式(即一个一个地返回其各种元素),并且您需要定义它是如何工作的。您将创建一个实现IEnumerator
. 但是你仍然需要IEnumerator
在IEnumerable
课堂上返回它。
For a look at what an enumerator (implementing IEnumerator<T>
) looks like, see any Enumerator<T>
class, such as the ones contained in List<T>
, Queue<T>,
or Stack<T>
. For a look at a class implementing IEnumerable
, see any standard collection class.
要查看枚举器(实现IEnumerator<T>
)的样子,请查看任何Enumerator<T>
类,例如包含在List<T>
、Queue<T>,
或 中的类Stack<T>
。要查看实现 的类IEnumerable
,请参阅任何标准集合类。
回答by SLaks
An Enumerator
shows you the items in a list or collection.
Each instance of an Enumerator is at a certain position (the 1st element, the 7th element, etc) and can give you that element (IEnumerator.Current
) or move to the next one (IEnumerator.MoveNext
). When you write a foreach
loop in C#, the compiler generates code that uses an Enumerator.
一个Enumerator
你在列表中显示或集合的项目。Enumerator 的每个实例都位于某个位置(第 1 个元素、第 7 个元素等),并且可以为您提供该元素 ( IEnumerator.Current
) 或移动到下一个 ( IEnumerator.MoveNext
)。foreach
在 C# 中编写循环时,编译器会生成使用枚举器的代码。
An Enumerable
is a class that can give you Enumerator
s. It has a method called GetEnumerator
which gives you an Enumerator
that looks at its items. When you write a foreach
loop in C#, the code that it generates calls GetEnumerator
to create the Enumerator
used by the loop.
AnEnumerable
是一个可以给你Enumerator
s 的类。它有一个名为的方法GetEnumerator
,它为您提供了一个Enumerator
查看其项目的方法。当您foreach
在 C# 中编写循环时,它生成的代码调用GetEnumerator
以创建Enumerator
循环使用的代码。