This is lesson 8 of 12 in this module Course 93% complete

For versus Foreach Performance

Premium Content

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

The lesson contrasts the indexed for loop with the foreach keyword, then dives into how foreach compiles into an enumerator object with MoveNext and Current, explaining that enumerators are designed for sequentially traversing potentially infinite or memory-unfriendly data structures such as database result sets. We establish that enumerator overhead makes foreach significantly slower than for on in-memory collections.