<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Enumerating over arrays with for vs. foreach</title><link>http://blogs.msdn.com/ricom/archive/2004/04/29/123126.aspx</link><description>Brad Abrams forwarded an interesting question to me this morning Questions: What is the difference between enumerating an array using for versus foreach ? What is the recommended practice and why? I did a quick analysis for him which he has just posted</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Enumerating over arrays with for vs. foreach</title><link>http://blogs.msdn.com/ricom/archive/2004/04/29/123126.aspx#123545</link><pubDate>Fri, 30 Apr 2004 02:42:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:123545</guid><dc:creator>Slavo Furman</dc:creator><description>Rico, and what do you say about Joshua Allen's similar analysis on using loops?&lt;br&gt;&lt;br&gt;&amp;quot;Loopy Decisions&amp;quot;&lt;br&gt;(&lt;a target="_new" href="http://www.netcrucible.com/blog/PermaLink.aspx?guid=1becbd8e-5b9f-40f0-bddf-994aeb580028"&gt;http://www.netcrucible.com/blog/PermaLink.aspx?guid=1becbd8e-5b9f-40f0-bddf-994aeb580028&lt;/a&gt;)&lt;br&gt;&lt;br&gt;thanks,&lt;br&gt;Slavo.&lt;br&gt;</description></item><item><title>re: Enumerating over arrays with for vs. foreach</title><link>http://blogs.msdn.com/ricom/archive/2004/04/29/123126.aspx#123554</link><pubDate>Fri, 30 Apr 2004 02:52:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:123554</guid><dc:creator>Rico Mariani</dc:creator><description>Well basically he's saying the exact same thing as me.  There's no difference between the first two cases and in any case there isn't ever likely to be enough difference that you should change your coding style.&lt;br&gt;&lt;br&gt;But the most important thing he says is that, whatever you do, if you're going to make a change like this, do measure it in your own scenarios to see what you'd be getting.  Who knows, maybe in a more complex function the optimizer will have a heart-attack and not be able to reduce the code down properly.&lt;br&gt;&lt;br&gt;For my part, I just would like to remind you that this is all about arrays and not ArrayLists or other collection classes.&lt;br&gt;&lt;br&gt;Quoteing from my other message:&lt;br&gt;&lt;br&gt;See the &amp;quot;Enumeration Overhead&amp;quot; section in &lt;a target="_new" href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/scalenetchapt05.asp"&gt;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/scalenetchapt05.asp&lt;/a&gt; -- searching for &amp;quot;foreach&amp;quot; in that same document will give other illustrations and guidance.</description></item><item><title>re: Enumerating over arrays with for vs. foreach</title><link>http://blogs.msdn.com/ricom/archive/2004/04/29/123126.aspx#125538</link><pubDate>Tue, 04 May 2004 08:51:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:125538</guid><dc:creator>nfactorial</dc:creator><description>Hi Rico,&lt;br&gt;  A question related to this has been occuring to me also. Whilst the above makes much sense, enumerating an array occurs far less frequently than enumerating a [custom] collection. When I say custom collection, I mean a total, type-safe collection (rather than one being a simple wrapper around ArrayList or something).&lt;br&gt;&lt;br&gt;Ie. The data members of such a collection may look similar to:&lt;br&gt;&lt;br&gt;private const int    DefaultCapacity = 16;&lt;br&gt;private int          count = 0;&lt;br&gt;private Item[]       itemList;&lt;br&gt;&lt;br&gt;Where itemList expands to reach contain items as they are added. This means that count will never exceed itemList.Length. However, I would guess that the JIT wont be clever enough to reach this conclusion and thus add the bounds checking. Even when an index is made on the collection:&lt;br&gt;&lt;br&gt;public Item this[ int index ]&lt;br&gt;{&lt;br&gt;    get&lt;br&gt;    {&lt;br&gt;        if ( index &amp;lt; 0 || index &amp;gt;= count )&lt;br&gt;            throw new IndexOutOfRange...&lt;br&gt;&lt;br&gt;        return itemList[ index ];&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;&lt;br&gt;I already put a bounds check here obviously, but because I'm using 'count' instead of 'itemList.Length' would there be another one added by the JIT? Of course I could look at the debug output, and I will if it becomes a problem for me (at the moment, I'm just interested to see if it may be an issue :)&lt;br&gt;&lt;br&gt;Also, if the above does pose a problem for the JIT, is this still applicable when using generic collection in Whidbey. Or is the JIT more intellegent with them (I don't have access to Whidbey atm)?&lt;br&gt;&lt;br&gt;I'm more interested in the foreach case in custom collections, as I generally use foreach in this case rather than iterating over them using for.&lt;br&gt;&lt;br&gt;Thanks,&lt;br&gt;n!</description></item><item><title>re: Enumerating over arrays with for vs. foreach</title><link>http://blogs.msdn.com/ricom/archive/2004/04/29/123126.aspx#125690</link><pubDate>Tue, 04 May 2004 14:12:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:125690</guid><dc:creator>Rico Mariani</dc:creator><description>The jit wouldn't have any way of knowing that your &amp;quot;count&amp;quot; is strictly less than the array length in all cases so yes I'd expect you to incur a second test until maybe some day when we could do some very deep analysis and still jit fast enough.&lt;br&gt;&lt;br&gt;You could remove the index &amp;lt; 0 test if you're willing to throw the array bounds check exception instead.&lt;br&gt;&lt;br&gt;Depending on your use of the collection it may or may not make a difference.  I'd expect most times it wouldn't end up mattering much.  Usage is everything.</description></item><item><title>re: Enumerating over arrays with for vs. foreach</title><link>http://blogs.msdn.com/ricom/archive/2004/04/29/123126.aspx#125691</link><pubDate>Tue, 04 May 2004 14:14:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:125691</guid><dc:creator>Rico Mariani</dc:creator><description>By the way, what you have looks remarkably like System.ArrayList I was wondering why you didn't just use that?</description></item><item><title>re: Enumerating over arrays with for vs. foreach</title><link>http://blogs.msdn.com/ricom/archive/2004/04/29/123126.aspx#125798</link><pubDate>Tue, 04 May 2004 16:51:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:125798</guid><dc:creator>nfactorial</dc:creator><description>Thanks for the notes! I thought it'd be too much for the JIT but just wondered if I was underestimating it :)&lt;br&gt;&lt;br&gt;As for not using ArrayList, it was only an example :) But I usually write the above for value type collections to avoid the boxing penalty of System.ArrayList. As I said, no whidbey\generics as of yet :(&lt;br&gt;&lt;br&gt;Thanks again,&lt;br&gt;n!</description></item><item><title>re: Enumerating over arrays with for vs. foreach</title><link>http://blogs.msdn.com/ricom/archive/2004/04/29/123126.aspx#125802</link><pubDate>Tue, 04 May 2004 16:57:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:125802</guid><dc:creator>Rico Mariani</dc:creator><description>Gotcha :)</description></item><item><title>re: Enumerating over arrays with for vs. foreach</title><link>http://blogs.msdn.com/ricom/archive/2004/04/29/123126.aspx#142948</link><pubDate>Thu, 27 May 2004 09:41:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:142948</guid><dc:creator>nfactorial</dc:creator><description>Hi again,&lt;br&gt;Sorry to come back to this :) I was thinking, is it possible to provide a hint to the JIT that a local count *is* in actual fact less than the length of the array. Thus avoiding the bounds check each time.&lt;br&gt;&lt;br&gt;For instance, if I have a custom collection and within a collection method I need to scan the array:&lt;br&gt;&lt;br&gt;public class MyCollection&lt;br&gt;{&lt;br&gt;	private int	count	= 0;&lt;br&gt;	private int[]	itemList;&lt;br&gt;&lt;br&gt;	... yadda yadda ...&lt;br&gt;&lt;br&gt;	public bool Contains( int someValue )&lt;br&gt;	{&lt;br&gt;		for ( int loop = 0; loop &amp;lt; count; ++loop )&lt;br&gt;		{&lt;br&gt;			if ( itemList[ loop ] == someValue )&lt;br&gt;				return true;&lt;br&gt;		}&lt;br&gt;&lt;br&gt;		return false;&lt;br&gt;	}&lt;br&gt;}&lt;br&gt;&lt;br&gt;I will get the bounds check, would the JIT understand something like:&lt;br&gt;&lt;br&gt;public bool Contains( int someValue )&lt;br&gt;{&lt;br&gt;	if ( count &amp;gt; 0 &amp;amp;&amp;amp; count &amp;lt;= itemList.Length )&lt;br&gt;	{&lt;br&gt;		for ( int loop = 0; loop &amp;lt; count; ++loop )&lt;br&gt;		{&lt;br&gt;			if ( itemList[ loop ] == someValue )&lt;br&gt;				return true;&lt;br&gt;		}&lt;br&gt;	}&lt;br&gt;&lt;br&gt;	return false;&lt;br&gt;}&lt;br&gt;&lt;br&gt;Although the first 'if' statement essentially does nothing (I know count is within this range), would the JIT take the hint? Again, please ignore the uselessness of this example :)&lt;br&gt;I'm thinking it doesn't (would be nice if it did), but could well be wrong :)&lt;br&gt;&lt;br&gt;Thanks,&lt;br&gt;n!</description></item><item><title>The Bum Wrap of foreach</title><link>http://blogs.msdn.com/ricom/archive/2004/04/29/123126.aspx#169399</link><pubDate>Wed, 30 Jun 2004 06:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:169399</guid><dc:creator>Brad Abrams </dc:creator><description /></item><item><title>The Bum Wrap of foreach</title><link>http://blogs.msdn.com/ricom/archive/2004/04/29/123126.aspx#169974</link><pubDate>Wed, 30 Jun 2004 20:22:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:169974</guid><dc:creator>dotRob</dc:creator><description /></item><item><title>The Bum Rap of foreach</title><link>http://blogs.msdn.com/ricom/archive/2004/04/29/123126.aspx#170216</link><pubDate>Wed, 30 Jun 2004 23:57:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:170216</guid><dc:creator>Brad Abrams </dc:creator><description /></item><item><title>re: Enumerating over arrays with for vs. foreach</title><link>http://blogs.msdn.com/ricom/archive/2004/04/29/123126.aspx#170691</link><pubDate>Thu, 01 Jul 2004 08:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:170691</guid><dc:creator>仪表</dc:creator><description>I think it's same thing below.&lt;br&gt;&lt;br&gt;Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();&lt;br&gt;&lt;br&gt; &lt;br&gt;&lt;br&gt;for (int i=0; i&amp;lt;assemblies.Length; i++)&lt;br&gt;&lt;br&gt;      DoSomething(assemblies[i]);&lt;br&gt;&lt;br&gt; &lt;br&gt;&lt;br&gt;foreach (Assembly a in assemblies)&lt;br&gt;&lt;br&gt;      DoSomething(a);&lt;br&gt;</description></item></channel></rss>