For versus Foreach Performance
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.
Previous Module
Basic Performance Optimizations
This Module
Array Performance Optimizations