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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:30:43  来源:igfitidea点击:

What is the difference between IEnumerator and IEnumerable?

c#.netienumerableienumerator

提问by

Possible Duplicate:
Can anyone explain IEnumerable and IEnumerator to me?

可能的重复:
谁能向我解释 IEnumerable 和 IEnumerator?

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();

example code from codebetter.com

来自 codebetter.com 的示例代码

回答by James Kingsbery

IEnumerableand IEnumeratorare both interfaces. IEnumerablehas 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). IEnumerablehas just one method whereas IEnumeratorhas 2 methods (MoveNextand Reset) and a property Current. For easy understanding consider IEnumerableas a box that contains IEnumeratorinside it (though not through inheritance or containment). See the code for better understanding:

IEnumerable并且IEnumerator都是接口。IEnumerable只有一种方法叫做GetEnumerator. 这个方法返回(因为所有方法都返回一些东西,包括 void)另一种类型,它是一个接口,该接口是IEnumerator. 当您在任何集合类中实现枚举器逻辑时,您将实现IEnumerable(泛型或非泛型)。IEnumerable只有一个方法,而IEnumerator有 2 个方法(MoveNextReset)和一个属性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 IEnumeratoris a thing that can enumerate: it has the Currentproperty and the MoveNextand Resetmethods (which in .NET code you probably won't call explicitly, though you could).

一个IEnumerator是可以列举一件事:它有Current属性和MoveNextReset方法(在.NET代码,你可能不显式调用,尽管你可以)。

An IEnumerableis 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 IEnumeratoris 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 IEnumeratorin an IEnumerableclass.

你用哪个?使用的唯一原因IEnumerator是,如果您有一些非标准的枚举方式(即一个一个地返回其各种元素),并且您需要定义它是如何工作的。您将创建一个实现IEnumerator. 但是你仍然需要IEnumeratorIEnumerable课堂上返回它。

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 Enumeratorshows 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 foreachloop in C#, the compiler generates code that uses an Enumerator.

一个Enumerator你在列表中显示或集合的项目。Enumerator 的每个实例都位于某个位置(第 1 个元素、第 7 个元素等),并且可以为您提供该元素 ( IEnumerator.Current) 或移动到下一个 ( IEnumerator.MoveNext)。foreach在 C# 中编写循环时,编译器会生成使用枚举器的代码。

An Enumerableis a class that can give you Enumerators. It has a method called GetEnumeratorwhich gives you an Enumeratorthat looks at its items. When you write a foreachloop in C#, the code that it generates calls GetEnumeratorto create the Enumeratorused by the loop.

AnEnumerable是一个可以给你Enumerators 的类。它有一个名为的方法GetEnumerator,它为您提供了一个Enumerator查看其项目的方法。当您foreach在 C# 中编写循环时,它生成的代码调用GetEnumerator以创建Enumerator循环使用的代码。