Hi Guys
NP97 GetEnumerator()
http://www.java2s.com/Tutorial/CSharp/0220__Data-Structure/Getitsenumeratorforanarray.htm
Following program is in the above website. I wish to know the function of
IEnumerator ie = intArray.GetEnumerator();.In other words what it does.
Please explain.
Thank you
using System;
using System.Collections;
class MainClass
{
static void
{
int[] intArray = { 10, 11, 12, 13 };
IEnumerator ie = intArray.GetEnumerator();
while (ie.MoveNext() == true)
{
int i = (int)ie.Current;
Console.WriteLine("{0}", i);
}
}
}
/*
10
11
12
13
*/
Posted Apr 23, 2008, 2:10 PM
Thank you for your explanation, Alan
AlanPosted Apr 23, 2008, 1:57 PM
As the name suggests a class which Implements the IEnumerator interface is used to enumerate a collection.
The class must implement the MoveNext() and Reset() methods and the Current property.
Enumerators are used in the background to facilitate the foreach() statement and it's unusual for them to be used explicitly.
However,in the code you posted, the enumerator for an integer array is obtained directly (by calling the GetEnumerator() method) and is then used explicitly to enumerate the array using a while loop.