Possible Duplicate:
For vs Foreach loop in C#
What is the major difference between 'for each' and 'for' loops in .NET? Is there any performance gain while comparing these two? Which one gives a better performance/faster/memory management?
Possible Duplicate:
For vs Foreach loop in C#
What is the major difference between 'for each' and 'for' loops in .NET? Is there any performance gain while comparing these two? Which one gives a better performance/faster/memory management?
See The Code Project article foreach vs. for (C#)*.
foreach
is thinking about everything as a collection and is treating them as a collection. That will also reduce the performance of the work.
To write high performance code that is not for collections, use a for
loop.
Even for collections, foreach
may look handy when using, but it's not that efficient. Therefore, I strongly recommend everyone to use a for
loop rather than foreach
at any stage.