This is lesson 9 of 12 in this module Course 95% complete

Foreach on an Array

Premium Content

Sign in with your account or join us to access this lesson.

We reveal that the C# compiler silently rewrites foreach on a one-dimensional array into an equivalent for loop, so there is no performance difference between the two constructs for arrays. The lesson explains that this optimization only applies to 1D arrays; foreach on a List still generates a full enumerator. We use the compiled intermediate language code to prove both cases.