For-loop can be converted into foreach-loop suggestion in R# 5.0


I noticed new suggestion in R# 5.0 which suggest me to convert for-loop into foreach-loop for my array.

There are performance penalty in such suggestion (you can check yourself). Look at Kevin Ransom: To foreach or not to foreach that is the question.


"Recently email was forwarded to me with a link to a page with some performance tips for developers.  The second performance tip on the page was:


foreach through an array is incredibly slow compared to for (int i = 0; i < array.Length; i)

This one leapt out at me because it is well ... not to put too fine a point on it ... wrong!  (at least that is what I thought).  C# and Visual Basic 7 both contain specific optimizations to ensure that  foreach on arrays perform equivalently to for(int i=0; i < array.Length; i); I have written plenty of code assuming foreach and for(int i; ...) were equivalent.  To verify my assumption and to ensure that the code I wrote was as efficient as possible in the future I did a little experiment: I wrote a small C# application with 2 functions the first of which enumerated an array of integers using foreach, the second enumerated an array of integers using for(int ...).  I then used ildasm to disassemble the compiled binary to IL and then examined the resulting code."

You may also read Patterns & Practices: Improving .Net Application Performance

"Use for Instead of foreach in Performance-Critical Code Paths

Use for instead of foreach (C#) to iterate the contents of collections in performance-critical code. foreach in C# and For Each in Visual Basic .NET use an enumerator to provide enhanced navigation through arrays and collections. For more information, see "Enumeration Overhead" in the "Collection Guidelines" section later in this chapter."

Аlthough I quoted very old topics they still valid in general. You can find many posts on this topic in Internet. Maybe you will interest in discussion here: For vs Foreach loop in C#

Of course there are many nuances, but I suggest in general make additional check and do not suggest this conversion for arrays (or similar operation context) but still suggest for other collections.

Comment actions Permalink

I agree with the fact that for outperforms foreach but I don't agree with the fact that ReSharper should care about it. ReSharper is a refactoring and a productivity enhancement tool, not an code optimization tool. Although I would not mind to see these kind of pattern checks or best practices in an add-in for ReSharper.

Comment actions Permalink

I just ask do not suggest convert "for" to "foreach" for arrays (and do not suggest vice versa, simply do not apply this suggestion at least for "Type[]"). I understand that this suggestion was introduced to improve "readability" of code and general move toward LINQ but if you check R#'s suggestions list you'll find different areas where it applicable including "light" code optimization suggestions.

In general I try to avoid suggest features to R# which tides with deep code optimization since it completly different area but here I suppose it not hard to implement over already existing suggestion, so I try to ask before disabling this suggestion. At least it can be option for this suggestion.

Comment actions Permalink

No seriously such an add-in would be great. I know that in certain conditions by changing the order of the objects in an if-statement when you have an assignment in the next line that the compiler will use the IL DUP instruction. It would be nice to have an add-in that shows this kind of stuff and that make it easy to change the code using the action list :-)

Comment actions Permalink

I totally agree with you because there are a lot of performance details I always try to keep in mind during development (I specialize in highload server apps where performance is critical). It will be handy to have such add-in, but this is "offtopic"   R# just does his work so great that always want it done even more simplifying development or extend it "better code" concept toward performance hints even more, maybe it just because  specific of my work

In general, .NET compiler does his work good, and  troubles we both mention some kind of "exception" soon or late will be fixed by .NET Performance Team. I call them "light code optimization" and think it well suited general R# line. It already checks in its hints, suggestions a bit of similar to FxCop or general MS guidelines (e.g. Patterns and Practices) and specialized performance add-in would be great but as you mention in first answer it come out main concept.

"Deep" optimization require workflow analysis and many other operations and definitely not R# task.


Please sign in to leave a comment.