In what case would it make sense for me to forgo an array using loops
"normal" ( for/foreach/while
) via index to use IEnumerator
as shown in Example 3?
Example1
//Usando for
int[] array = new int[]{1, 2, 3, 4, 5 };
for ( int i = 0; i < array.Lenght; i++)
{
Console.WriteLine( array[i] );
}
Example2
//Usando foreach (Na prática o compilador gera um código bem parecido ao Exemplo1)
int[] array = new int[]{1, 2, 3, 4, 5};
foreach (int item in array)
{
Console.WriteLine(item);
}
Example3
//Via IEnumerator utilizando o loop while
int[] array = new int[]{1, 2, 3, 4, 5 };
IEnumerator o = array.GetEnumerator();
while (o.MoveNext())
{
Console.WriteLine(o.Current);
}