foreach
loops demonstrate more specific intent than for
loops.
Using a foreach
loop demonstrates to anyone using your code that you are planning to do something to each member of a collection irrespective of its place in the collection. It also shows you aren't modifying the original collection (and throws an exception if you try to).
The other advantage of foreach
is that it works on any IEnumerable
, where as for
only makes sense for IList
, where each element actually has an index.
However, if you need to use the index of an element, then of course you should be allowed to use a for
loop. But if you don't need to use an index, having one is just cluttering your code.
There are no significant performance implications as far as I'm aware. At some stage in the future it might be easier to adapt code using foreach
to run on multiple cores, but that's not something to worry about right now.