본문 바로가기

Software/C#.Net

[C#]IEnumerator Enumerator

IEnumerator has the following characteristics:
member of the System.Collections namespace
it contains three members Current,MoveNext,Reset

example
using System.Collections;
class MyEnumerator:IEnumerator
{
   public object Current{ get;}
   public bool MoveNext{ ... }
   public void Reset{ ...}
   .....
}


using System.Collections;
class ColorEnumerator: IEnumerator
{
string[] Colors; Implements IEnumerator
int Position = -1;
public object Current // Current
{
get { return Colors[Position]; }
}
public bool MoveNext() // MoveNext
{
if (Position < Colors.Length - 1)
{
Position++;
return true;
}
else
return false;
}
public void Reset() // Reset
{
Position = -1;
}
public ColorEnumerator(string[] theColors) // Constructor
{
Colors = new string[theColors.Length];
for (int i = 0; i < theColors.Length; i++)
Colors[i] = theColors[i];
}
}

IEnumerable Interface
IEnumerable interface has only single member method GetEnumerator()
return value : enumerator for object