3
In which case it would make sense for me to give up iterating a array using loops
"normal" (for/foreach/while
) via index to use the 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] );
}
Exemplo2
//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);
}