<p>I have been told that there is a performance difference between the following code blocks.</p>

<pre><code>foreach (Entity e in entityList)
{
 ....
}
</code></pre>

<p>and </p>

<pre><code>for (int i=0; i&lt;entityList.Count; i++)
{
   Entity e = (Entity)entityList[i];
   ...
}
</code></pre>

<p>where</p>

<pre><code>List&lt;Entity&gt; entityList;
</code></pre>

<p>I am no CLR expect but from what I can tell they should boil down to basically the same code.  Does anybody have concrete (heck, I'd take packed dirt) evidence one way or the other?</p>

[{'answer_id': 44225, 'author': 'Daniel Jennings', 'author_id': 3641, 'author_profile': 'https://Stackoverflow.com/users/3641', 'pm_score': 4, 'selected': True, 'text': "<p>foreach creates an instance of an enumerator (returned from GetEnumerator) and that enumerator also keeps state throughout the course of the foreach loop. It then repeatedly calls for the Next() object on the enumerator and runs your code for each object it returns.</p>\n\n<p>They don't boil down to the same code in any way, really, which you'd see if you wrote your own enumerator.</p>\n"}, {'answer_id': 44229, 'author': 'David Basarab', 'author_id': 2469, 'author_profile': 'https://Stackoverflow.com/users/2469', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://www.codeproject.com/KB/cs/foreach.aspx" rel="noreferrer">Here</a> is a good article that shows the IL differences between the two loops.</p>\n\n<p>Foreach is technically slower however much easier to use and easier to read.  Unless performance is critical I prefer the foreach loop over the for loop.</p>\n'}, {'answer_id': 44248, 'author': 'OwenP', 'author_id': 2547, 'author_profile': 'https://Stackoverflow.com/users/2547', 'pm_score': 0, 'selected': False, 'text': "<p>I think one possible situation where you <em>might</em> get a performance gain is if the enumerable type's size and the loop condition is a constant; for example:</p>\n\n<pre><code>const int ArraySize = 10;\nint[] values = new int[ArraySize];\n\n//...\n\nfor (int i = 0; i </pre>\n\n<p>In this case, depending on the complexity of the loop body, the compiler might be able to replace the loop with inline calls.  I have no idea if the .NET compiler does this, and it's of limited utility if the size of the enumerable type is dynamic.</p>\n\n<p>One situation where <code>foreach</code> might perform better is with data structures like a linked list where random access means traversing the list; the enumerator used by <code>foreach</code> will probably iterate one item at a time, making each access O(1) and the full loop O(n), but calling the indexer means starting at the head and finding the item at the right index; O(N) each loop for O(n^2).</p>\n\n<p>Personally I don't usually worry about it and use <code>foreach</code> any time I need all items and don't care about the index of the item.  If I'm not working with all of the items or I really need to know the index, I use for</code>.  The only time I could see it being a big concern is with structures like linked lists.</p>\n"}, {'answer_id': 44252, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>The foreach sample roughly corresponds to this code:</p>\n\n<pre><code>using(IEnumerator&lt;Entity&gt; e = entityList.GetEnumerator()) {\n    while(e.MoveNext()) {\n        Entity entity = e.Current;\n        ...\n    }\n}\n</code></pre>\n\n<p>There are two costs here that a regular for loop does not have to pay:</p>\n\n<ol>\n<li>The cost of allocating the enumerator object by entityList.GetEnumerator().</li>\n<li>The cost of two virtual methods calls (MoveNext and Current) for each element of the list.</li>\n</ol>\n'}, {'answer_id': 44263, 'author': 'Patrick', 'author_id': 429, 'author_profile': 'https://Stackoverflow.com/users/429', 'pm_score': 1, 'selected': False, 'text': '<p>In terms of allocations, it\'d be better to look at <a href="http://blogs.msdn.com/etayrien/archive/2007/03/17/foreach-garbage-and-the-clr-profiler.aspx" rel="nofollow noreferrer">this blogpost</a>. It shows in exactly in what circumstances an enumerator is allocated on the heap.</p>\n'}, {'answer_id': 44319, 'author': 'Brian Leahy', 'author_id': 580, 'author_profile': 'https://Stackoverflow.com/users/580', 'pm_score': 2, 'selected': False, 'text': '<p>One point missed here:\nA List has a Count property, it internally keeps track of how many elements are in it.</p>\n\n<p>An IEnumerable DOES NOT.  </p>\n\n<p>If you program to the interface IEnumerable and use the count extention method it will enumerate just to count the elements.  </p>\n\n<p>A moot point though since in the IEnumerable you cannot refer to items by index.</p>\n\n<p>So if you want to lock in to Lists and Arrays you can get small performance increases.</p>\n\n<p>If you want flexability use foreach and program to IEnumerable. (allowing the use of linq and/or yield return). </p>\n'}, {'answer_id': 21084968, 'author': 'kavitha Reddy', 'author_id': 3073215, 'author_profile': 'https://Stackoverflow.com/users/3073215', 'pm_score': 0, 'selected': False, 'text': '<pre><code>For Loop\nfor loop is used to perform the opreration n times\nfor(int i=0;i&lt;n;i++)\n{\nl=i;\n}\nforeach loop\n\nint[] i={1,2,3,4,5,6}\nforeach loop is used to perform each operation value/object in IEnumarable \nforeach(var k in i)\n{\nl=k;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/44220', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2894/']