On List<T> this matters in tight loops.
❌
foreach (var item in list) { }
✅ Faster
for (int i = 0; i < list.Count; i++) { }
Why
-
foreachcreates an enumerator -
foris index-based and faster
Use this only in hot paths, not everywhere.
Daily micro-tips for C#, SQL, performance, and scalable backend engineering.
On List<T> this matters in tight loops.
foreach (var item in list) { }
✅ Faster
for (int i = 0; i < list.Count; i++) { }
Why
foreach creates an enumerator
for is index-based and faster
Use this only in hot paths, not everywhere.