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

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.

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