Is foreach purely “syntactic sugar”?

foreach is internally just a while loop that calls the methods in IEnumerator.


It's purely syntactic sugar in that you could obtain the same behaviour without it, yes. Many other things are the same... for, while etc... To misquote Archimedes: "Give me if and goto, and I will move the code..."

No, the CLR doesn't have any concept of foreach.


It is syntactic sugar. However, note that foreach works by calling GetEnumerator(), then MoveNext() until there is no further item returned and then always calls Dispose() on the enumerator it previously obtained. If you want to do it the same way, don't forget that Dispose()!

Also, the CLR does some tricks related to getting the enumerator. See here and here, for example.