Demo: For and Foreach in Intermediate Language
The lesson disassembles the benchmark DLL to confirm that for on a list calls List.GetItem, foreach on a list calls GetEnumerator and MoveNext, and both for and foreach on a 1D array produce identical ldelm-based loop code. We demonstrate exactly why the enumerator path is slower: every iteration requires a virtual method call to advance to the next element.
Previous Module
Basic Performance Optimizations
This Module
Array Performance Optimizations