In a video tutorial the instructor said not to fall into foolish to think that the foreach loop is one, and was vehement that he was an iterator.
There are cases where we can scroll through the items in an array using foreach as a "compact for" version.
That is, when we use foreach for arrays or arrays, in the background we are using it as a compact version of the for loop. When we use it to iterate collections, it is indeed a TRUE iterator because "access" IEnumerator methods
Is the statement correct? Can anyone add anything to this?
Following example code:
//foreach com Arrays
int[] array = new int[]{1, 2, 3, 4, 5, 6};
foreach (int item in array)
{
Console.WriteLine(item);
}
However, the compiler generates C # C # equivalent to the generated CIL:
//Código C# equivalente ao CIL gerado
int[] tempArray;
int[] array = new int[]{1, 2, 3, 4, 5, 6};
tempArray = array;
for (int counter = 0; (counter < tempArray.Length); counter++) {
int item = tempArray[counter];
Console.WriteLine(item);
}
The above code was taken from the book: Essential C-6.0, 5th Edition [Author Mark Michaelis] page 582