int[] MyArray = { 10,11,12,13};
IEnumerator enumerator = MyArray.GetEnumerator();
while(enumerator.MoveNext())
{
int i = (int)enumerator.Current;
Console.WriteLine($"Hello World!{i}");
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Spectrum : IEnumerable
{
string[] Colors = { "1e","ww", "1e", "ww", "1e", "ww", "1e", "ww", "1e", "ww",};
public IEnumerator GetEnumerator()
{
return new ColorEnumerator(Colors);
}
}
class ColorEnumerator : IEnumerator
{
string[] _colors;
int _position = -1;
public ColorEnumerator(string[] theColors)
{
_colors = new string[theColors.Length];
for (int i = 0; i < theColors.Length; i++)
{
_colors[i] = theColors[i];
}
}
public object Current
{
get
{
if (_position==-1)
{
throw new InvalidOperationException();
}
if (_position >= _colors.Length)
{
throw new InvalidOperationException();
}
return _colors[_position];
}
}
public bool MoveNext()
{
if (_position < _colors.Length - 1)
{
_position++;
return true;
}
else
{
return false;
}
}
public void Reset()
{
_position = -1;
}
}
}