This is lesson 12 of 12 in this module
Course 100% complete
For outperforms Foreach
The lesson summarizes when each construct is the right choice: use foreach only when you genuinely need the enumerator's ability to traverse large, dynamic, or memory-unfriendly data structures such as LINQ queries or infinite sequences; use for with an indexer for all in-memory collections where performance matters. We note that LINQ is entirely built on enumerators and therefore carries the same performance trade-off.
Previous Module
Basic Performance Optimizations
This Module
Array Performance Optimizations