Questions tagged [ienumerator]

IEnumerator and its generic counterpart IEnumerator are .NET interfaces which facilitate iteration through items in a collection.

The IEnumerator and IEnumerator<T> interfaces (in the System.Collections and System.Collections.Generic namespaces, respectively) facilitate iteration through items in a collection through three members:

  • The Current property, which returns an object for IEnumerator and a T for IEnumerator<T>.
  • The MoveNext() method, which returns a bool - true if the enumerator successfully moved to the next item; false if not. Note that the initial position of the enumerator is before the first item; MoveNext() must be called to check there are any items before Current should be used.
  • The Reset() method, which returns the state of the enumerator to its initial position (before the first item).

References:

384 questions
335
votes
16 answers

Can anyone explain IEnumerable and IEnumerator to me?

Can anyone explain IEnumerable and IEnumerator to me? For example, when to use it over foreach? what's the difference between IEnumerable and IEnumerator? Why do we need to use it?
prodev42
  • 6,389
  • 8
  • 32
  • 35
126
votes
4 answers

What is the difference between IEnumerator and IEnumerable?

Possible Duplicate: Can anyone explain IEnumerable and IEnumerator to me? What are the differences between IEnumerator and IEnumerable?
Shaun
109
votes
8 answers

obtain generic enumerator from an array

In C#, how does one obtain a generic enumerator from a given array? In the code below, MyArray is an array of MyType objects. I'd like to obtain MyIEnumerator in the fashion shown, but it seems that I obtain an empty enumerator (although I've…
JaysonFix
  • 2,515
  • 9
  • 28
  • 28
88
votes
5 answers

Simple IEnumerator use (with example)

I am having trouble remembering how (but not why) to use IEnumerators in C#. I am used to Java with its wonderful documentation that explains everything to beginners quite nicely. So please, bear with me. I have tried learning from other answers on…
BlackVegetable
  • 12,594
  • 8
  • 50
  • 82
85
votes
6 answers

Why does IEnumerator inherit from IDisposable while the non-generic IEnumerator does not?

I noticed that the generic IEnumerator inherits from IDisposable, but the non-generic interface IEnumerator does not. Why is it designed in this way? Usually, we use foreach statement to go through a IEnumerator instance. The generated code of…
Morgan Cheng
  • 73,950
  • 66
  • 171
  • 230
71
votes
9 answers

Return an empty IEnumerator

I have an interface that, among other things, implements a "public IEnumerator GetEnumerator()" method, so I can use the interface in a foreach statement. I implement this interface in several classes and in one of them, I want to return an empty…
David Božjak
  • 16,887
  • 18
  • 67
  • 98
63
votes
2 answers

Why do BCL Collections use struct enumerators, not classes?

We all know mutable structs are evil in general. I'm also pretty sure that because IEnumerable.GetEnumerator() returns type IEnumerator, the structs are immediately boxed into a reference type, costing more than if they were simply reference…
Eloff
  • 20,828
  • 17
  • 83
  • 112
51
votes
2 answers

Why do arrays in .net only implement IEnumerable and not IEnumerable?

I was implementing my own ArrayList class and was left surprised when I realised that public System.Collections.Generic.IEnumerator GetEnumerator() { return _array.GetEnumerator(); } didn't work. What is the reason arrays don't implement…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
36
votes
3 answers

How Can I Use IEnumerator.Reset()?

How exactly is the right way to call IEnumerator.Reset? The documentation says: The Reset method is provided for COM interoperability. It does not necessarily need to be implemented; instead, the implementer can simply throw a…
user541686
  • 205,094
  • 128
  • 528
  • 886
33
votes
3 answers

Is Yield Return == IEnumerable & IEnumerator?

Is yield return a shortcut for implementing IEnumerable and IEnumerator?
John
  • 5,381
  • 4
  • 30
  • 26
31
votes
5 answers

What is the best way to convert an IEnumerator to a generic IEnumerator?

I am writing a custom ConfigurationElementCollection for a custom ConfigurationHandler in C#.NET 3.5 and I am wanting to expose the IEnumerator as a generic IEnumerator. What would be the best way to achieve this? I am currently using the…
zonkflut
  • 2,929
  • 3
  • 22
  • 25
26
votes
1 answer

Convert CollectionBase to List or data type usable with Linq

I am using Aspose cells to manipulate Excel spreadsheets. One of the types in the API is a collection of Pictures in the spreadsheet, which derives from CollectionBase: see this…
ChrisCa
  • 10,876
  • 22
  • 81
  • 118
22
votes
5 answers

Can I have a method returning IEnumerator and use it in a foreach loop?

I need to set the height of every textbox on my form, some of which are nested within other controls. I thought I could do something like this: private static IEnumerator FindTextBoxes(Control rootControl) { foreach (Control control in…
littlecharva
  • 263
  • 1
  • 4
  • 5
21
votes
3 answers

Unity - IEnumerator's yield return null

I'm currently trying to understand IEnumerator & Coroutine within the context of Unity and am not too confident on what the "yield return null" performs. At the moment i believe it basically pauses and waits for the next frame and in the next frame…
WonderfulWonder
  • 515
  • 1
  • 8
  • 20
20
votes
4 answers

Why does capturing a mutable struct variable inside a closure within a using statement change its local behavior?

Update: Well, now I've gone and done it: I filed a bug report with Microsoft about this, as I seriously doubt that it is correct behavior. That said, I'm still not 100% sure what to believe regarding this question; so I can see that what is…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
1
2 3
25 26