using System.Collections;
internal class Program
{
public class MyList : IEnumerable
{
string[] people = { "Tom", "Sam", "Bob" };
public class MyEnumerator : IEnumerator
{
string[] people;
int index = -1;
public object Current => people[index];
public MyEnumerator(MyList list)
{
this.people = list.people;
}
public bool MoveNext()
{
index++;
return index < people.Length;
}
public void Reset()
{
index = -1;
}
}
public IEnumerator GetEnumerator()
{
return new MyEnumerator(this);
}
}
private static void Main(string[] args)
{
MyList people = new MyList();
IEnumerator peopleEnumerator = people.GetEnumerator(); // получаем IEnumerator
while (peopleEnumerator.MoveNext()) // пока не будет возвращено false
{
string item = (string)peopleEnumerator.Current; // получаем элемент на текущей позиции
Console.WriteLine(item);
}
peopleEnumerator.Reset(); // сбрасываем указатель в начало массива
}
}