<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Interesting (good) Behavior of Closures</title>
	<atom:link href="http://lostechies.com/chadmyers/2008/03/09/interesting-good-behavior-of-closures/feed/" rel="self" type="application/rss+xml" />
	<link>http://lostechies.com/chadmyers/2008/03/09/interesting-good-behavior-of-closures/</link>
	<description>Software development, testing, and techie life</description>
	<lastBuildDate>Thu, 08 Mar 2012 22:19:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
	<item>
		<title>By: Simone Busoli</title>
		<link>http://lostechies.com/chadmyers/2008/03/09/interesting-good-behavior-of-closures/#comment-155</link>
		<dc:creator>Simone Busoli</dc:creator>
		<pubDate>Fri, 14 Mar 2008 19:15:50 +0000</pubDate>
		<guid isPermaLink="false">/blogs/chad_myers/archive/2008/03/08/interesting-good-behavior-of-closures.aspx#comment-155</guid>
		<description>Hi Chad, yeah, that&#039;s a cool behavior, and has somewhat become intuitive, since as lambda - or an anonymous method - are pointers to methods, not actually code which gets executed, therefore they create a closure around the variables of the environment and actually use them only when they are invoked.

That&#039;s why, when you create loops which call anonymous methods, at times you need to declare an inner variable which disconnects from the outer closure and keeps the right value of the variable for each iterations, and the following example usually doesn&#039;t work as expected:

var handle = new ManualResetEvent(false);

for(int i = 0; i &lt; 1000000; i++)
    ThreadPool.QueueUserWorkItem(() =&gt;  
    { 
        handle.WaitOne();
        Console.WriteLine(i);
    });

handle.Set();

PS. Wrote it without the compiler, I&#039;m not sure it does, but you get the idea.</description>
		<content:encoded><![CDATA[<p>Hi Chad, yeah, that&#8217;s a cool behavior, and has somewhat become intuitive, since as lambda &#8211; or an anonymous method &#8211; are pointers to methods, not actually code which gets executed, therefore they create a closure around the variables of the environment and actually use them only when they are invoked.</p>
<p>That&#8217;s why, when you create loops which call anonymous methods, at times you need to declare an inner variable which disconnects from the outer closure and keeps the right value of the variable for each iterations, and the following example usually doesn&#8217;t work as expected:</p>
<p>var handle = new ManualResetEvent(false);</p>
<p>for(int i = 0; i < 1000000; i++)<br />
    ThreadPool.QueueUserWorkItem(() =><br />
    {<br />
        handle.WaitOne();<br />
        Console.WriteLine(i);<br />
    });</p>
<p>handle.Set();</p>
<p>PS. Wrote it without the compiler, I&#8217;m not sure it does, but you get the idea.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon Scolamiero</title>
		<link>http://lostechies.com/chadmyers/2008/03/09/interesting-good-behavior-of-closures/#comment-154</link>
		<dc:creator>Jon Scolamiero</dc:creator>
		<pubDate>Tue, 11 Mar 2008 17:33:52 +0000</pubDate>
		<guid isPermaLink="false">/blogs/chad_myers/archive/2008/03/08/interesting-good-behavior-of-closures.aspx#comment-154</guid>
		<description>I like this behavior as it lets us do some setup early on in a method without worrying that the variable my not be instantiated yet.</description>
		<content:encoded><![CDATA[<p>I like this behavior as it lets us do some setup early on in a method without worrying that the variable my not be instantiated yet.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
