The compiler compiles a foreach loop into something like a for loop when the foreach is used with an array. And the compiler compile
Yes, it is purely sugar. The following code
var MyList = new List() { 10 , 20 , 30 , 40, 50} ;
foreach(int i in MyList)
{
Console.WriteLine(i);
}
is translated in compiler as:
Ienumrator rator = MyList.GetEnumrator();
try
{
while(rator.MoveNext())
{
int i = rator.Current;
Console.WriteLine(i);
}
}
finally
{
rator.Dispose()
}