Skip to main content
This is lesson 9 of 12 in this module 9/12

Foreach on an Array

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.

Video lesson

Want to watch this lesson?

This lesson is part of the study plan. Get instant access to all lectures, labs and practice exams.

Get the Study Plan