<?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>Closing over the loop variable considered harmful</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx</link><description>(This is part one of a two-part series on the loop-variable-closure problem. Part two is here .) UPDATE : We are taking the breaking change. In C# 5, the loop variable of a foreach will be logically inside the loop, and therefore closures will close over</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Closing over the loop variable considered harmful</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx#10295432</link><pubDate>Thu, 19 Apr 2012 16:47:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10295432</guid><dc:creator>Fernando Pelliccioni</dc:creator><description>&lt;p&gt;How do you solve this?&lt;/p&gt;
&lt;p&gt;//---&lt;/p&gt;
&lt;p&gt;var actions = new List&amp;lt;Action&amp;gt;();&lt;/p&gt;
&lt;p&gt;for ( int counter = 0; counter &amp;lt; 10; ++counter )&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;actions.Add ( () =&amp;gt; Console.WriteLine(counter) );&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;foreach (var action in actions)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;action();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;//---&lt;/p&gt;
&lt;p&gt;The range-based-for (foreach) sugar is wrong, but you need a capture-by-value lambdas. &lt;/p&gt;
&lt;p&gt;Especially in a language that tries to support value semantics.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10295432" width="1" height="1"&gt;</description></item><item><title>re: Closing over the loop variable considered harmful</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx#10285910</link><pubDate>Wed, 21 Mar 2012 14:05:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10285910</guid><dc:creator>Kyralessa</dc:creator><description>&lt;p&gt;I just saw it claimed elsewhere that this change would be made in C# 5, and I came here to see if it were true. &amp;nbsp;It is! &amp;nbsp;Hooray! &amp;nbsp;I shall bid a not-so-fond farewell to the bugs this used to lead to.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10285910" width="1" height="1"&gt;</description></item><item><title>re: Closing over the loop variable considered harmful</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx#10208548</link><pubDate>Fri, 09 Sep 2011 14:30:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10208548</guid><dc:creator>Roman Starkov</dc:creator><description>&lt;p&gt;Yes, please, Eric, consider changing the scope of the foreach variable so that it isn&amp;#39;t shared between iterations.&lt;/p&gt;
&lt;p&gt;C# is generally fantastic at doing just the right thing, without introducing ambiguity and without the developers even fully understanding just what really happens behind the scenes. The foreach variable capturing is an unfortunate example of where this doesn&amp;#39;t happen.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10208548" width="1" height="1"&gt;</description></item><item><title>re: Closing over the loop variable considered harmful</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx#10208047</link><pubDate>Thu, 08 Sep 2011 19:06:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10208047</guid><dc:creator>Brian Gideon</dc:creator><description>&lt;p&gt;Something has got to change. &amp;nbsp;Otherwise, the StackOverflow developers are going to have to create a new site called closeoverloopvariable.stackexchange.com dedicated specifically to this problem. &amp;nbsp;It has to be one of the most asked questions on SO. &amp;nbsp;I bet there are literally thousands of them lurking there already.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10208047" width="1" height="1"&gt;</description></item><item><title>re: Closing over the loop variable considered harmful</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx#10197842</link><pubDate>Fri, 19 Aug 2011 15:46:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10197842</guid><dc:creator>hoodaticus</dc:creator><description>&lt;p&gt;Thanks for the clear explanation!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10197842" width="1" height="1"&gt;</description></item><item><title>re: Closing over the loop variable considered harmful</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx#10153374</link><pubDate>Wed, 13 Apr 2011 15:59:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10153374</guid><dc:creator>dmihailescu</dc:creator><description>&lt;p&gt;I say change it since few people used it knowingly.&lt;/p&gt;
&lt;p&gt;Other languages should follow suit too in the same VS release.&lt;/p&gt;
&lt;p&gt;The warning is too often overlooked by many peoples.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10153374" width="1" height="1"&gt;</description></item><item><title>re: Closing over the loop variable considered harmful</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx#9966754</link><pubDate>Sat, 20 Feb 2010 11:29:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9966754</guid><dc:creator>Danny Milosavljevic</dc:creator><description>&lt;p&gt;Python allows both ways:&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt;&amp;gt; f = []&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt;&amp;gt; for i in range(10): f.append(lambda : i)&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt;&amp;gt; f[0]()&lt;/p&gt;
&lt;p&gt;9&lt;/p&gt;
&lt;p&gt;On the other hand:&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt;&amp;gt; f = []&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt;&amp;gt; for i in range(10): f.append(lambda v = i: v)&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt;&amp;gt; f[0]()&lt;/p&gt;
&lt;p&gt;0&lt;/p&gt;
&lt;p&gt;I've called this &amp;quot;v&amp;quot; for clarity. Of course, this also works:&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt;&amp;gt; f = []&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt;&amp;gt; for i in range(10): f.append(lambda i = i: i)&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt;&amp;gt; f[0]()&lt;/p&gt;
&lt;p&gt;0&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9966754" width="1" height="1"&gt;</description></item><item><title>re: Closing over the loop variable considered harmful</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx#9962931</link><pubDate>Fri, 12 Feb 2010 23:32:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9962931</guid><dc:creator>NetMage</dc:creator><description>&lt;p&gt;I vote for making the change.&lt;/p&gt;
&lt;p&gt;I think adding a warning or an error is the worst possible thing to do - I hate it when the message I get from a system is &amp;quot;you did this wrong, we know what you did wrong, we would rather tell you then do the right thing for you.&amp;quot; - that's not the kind of system I enjoy using, be it programming language or business application.&lt;/p&gt;
&lt;p&gt;This one may go even further - &amp;quot;we tricked you into doing the wrong thing, we know what you did was wrong, but we'd rather tell you about it than do the right thing for you.&amp;quot;&lt;/p&gt;
&lt;p&gt;All kinds of annoyance there. The VB error message is exactly that. See Alan Cooper for how error messages should be (mainly, don't have them).&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9962931" width="1" height="1"&gt;</description></item><item><title>re: Closing over the loop variable considered harmful</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx#9952295</link><pubDate>Sat, 23 Jan 2010 04:56:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9952295</guid><dc:creator>John Rose</dc:creator><description>&lt;p&gt;@Konrad: &amp;quot;I much prefer Java’s rule here: only allow closing over variables which are declared `final`. But then, this only works because Java allows local variables to be declared `final`, which C# doesn’t.&amp;quot;&lt;/p&gt;
&lt;p&gt;The reason Java supports final variables is exactly to fix this problem. &amp;nbsp;The designer of inner classes was a Lisp refugee who had been bitten by the DOTIMES issue (see Peter Seibel above), and didn't want to allow the problem into Java.&lt;/p&gt;
&lt;p&gt;This particular barn door is hard to close after the mutable iteration variable has galloped away.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9952295" width="1" height="1"&gt;</description></item><item><title>re: Closing over the loop variable considered harmful</title><link>http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx#9947605</link><pubDate>Wed, 13 Jan 2010 07:36:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9947605</guid><dc:creator>Cheap Facebook Developer</dc:creator><description>&lt;p&gt;Issue a warning in C#4, and change it in C#5.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9947605" width="1" height="1"&gt;</description></item></channel></rss>