<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://thoughtshapes.com" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>ThoughtShapes - .NET Delegate Recipes - Comments</title>
 <link>http://thoughtshapes.com/node/69</link>
 <description>Comments for &quot;.NET Delegate Recipes&quot;</description>
 <language>en</language>
<item>
 <title>Sweet!  Thanks!</title>
 <link>http://thoughtshapes.com/node/69#comment-589</link>
 <description>&lt;p&gt;Sweet!  Thanks!&lt;/p&gt;
</description>
 <pubDate>Wed, 31 Oct 2007 14:34:09 -0400</pubDate>
 <dc:creator>jay</dc:creator>
 <guid isPermaLink="false">comment 589 at http://thoughtshapes.com</guid>
</item>
<item>
 <title>You sure do get it Jay. But</title>
 <link>http://thoughtshapes.com/node/69#comment-588</link>
 <description>&lt;p&gt;You sure do get it Jay. But I kinda already knew that.&lt;/p&gt;
&lt;p&gt;I am thrilled that the post was so timely and helped.&lt;/p&gt;
</description>
 <pubDate>Wed, 31 Oct 2007 14:26:52 -0400</pubDate>
 <dc:creator>Rjae Easton</dc:creator>
 <guid isPermaLink="false">comment 588 at http://thoughtshapes.com</guid>
</item>
<item>
 <title>I hope you don&#039;t mind, but I</title>
 <link>http://thoughtshapes.com/node/69#comment-587</link>
 <description>&lt;p&gt;I hope you don&#039;t mind, but I replaced &amp;lt;code&amp;gt; with &amp;lt;pre class=&quot;code cs&quot;&amp;gt; to retain your formatting.&lt;/p&gt;
</description>
 <pubDate>Wed, 31 Oct 2007 14:25:35 -0400</pubDate>
 <dc:creator>Rjae Easton</dc:creator>
 <guid isPermaLink="false">comment 587 at http://thoughtshapes.com</guid>
</item>
<item>
 <title>sorry for the formatting of</title>
 <link>http://thoughtshapes.com/node/69#comment-586</link>
 <description>&lt;p&gt;sorry for the formatting of the code.&lt;br /&gt;
There were spaces there when I hit post :P&lt;/p&gt;
</description>
 <pubDate>Wed, 31 Oct 2007 13:16:00 -0400</pubDate>
 <dc:creator>jay</dc:creator>
 <guid isPermaLink="false">comment 586 at http://thoughtshapes.com</guid>
</item>
<item>
 <title>I get it!
So I must admit</title>
 <link>http://thoughtshapes.com/node/69#comment-585</link>
 <description>&lt;p&gt;I get it!&lt;br /&gt;
So I must admit that the terms Closure and lexical context threw me off at first.  But when I replaced them with Scope and Stack (and happened to be using something like this in a unit test I was writing) it coalesced for me.&lt;/p&gt;
&lt;p&gt;My example was this:&lt;/p&gt;
&lt;pre class=&quot;code cs&quot;&gt;[Test]
public SomeWorkRelatedUnitTestIWasWorkingOnThatTestsThreadSafetyOfACertainCall()
{
      // Some Mocking initialization goes here
      //....

      List autos = new List();
      for (int i = 0; i &amp;lt; 1; i++)
      {
          autos.Add(new AutoResetEvent(false));                
            ThreadPool.QueueUserWorkItem(
                delegate
                    {
                        myWorker.InitializeProcessors(true, mySettings);
                        autos[i].Set();
                    });
       }
       WaitHandle.WaitAll(autos.ToArray());
       myMocks.VerifyAllExpectationsHaveBeenMet();
}
&lt;/pre&gt;&lt;p&gt;
When I ran my test I was getting an exception at the autos[i].Set() call.  at this point i was 1, when I expected it to be 0.  I remember reading this post this morning and hopped on back to see what I was missing.&lt;/p&gt;
&lt;p&gt;Rjae&#039;s words (once I understood them) were exactly what was happening to me.  i was continuing to increment while the work was being done, because the delegate has a view into the scope that i lives at.&lt;br /&gt;
When I switched to something like:&lt;/p&gt;
&lt;pre class=&quot;code cs&quot;&gt;[Test]
public SomeWorkRelatedUnitTestIWasWorkingOnThatTestsThreadSafetyOfACertainCall()
{
     List autos = new List();
     for (int i = 0; i &amp;lt; 1; i++)
     {
         int anotherI = i;
         autos.Add(new AutoResetEvent(false));                
           ThreadPool.QueueUserWorkItem(
              delegate
                    {
                        myWorker.InitializeProcessors(true, mySettings);
                        autos[anotherI].Set();
                    });
      }
      WaitHandle.WaitAll(autos.ToArray());
}
&lt;/pre&gt;&lt;p&gt;It really became clear to me what Rjae was saying.  It&#039;s almost like a delegate gets it&#039;s own &quot;local(ish)&quot; storage.&lt;/p&gt;
&lt;p&gt;Thanks Rjae for the timely post!&lt;/p&gt;
</description>
 <pubDate>Wed, 31 Oct 2007 13:14:00 -0400</pubDate>
 <dc:creator>jay</dc:creator>
 <guid isPermaLink="false">comment 585 at http://thoughtshapes.com</guid>
</item>
<item>
 <title>.NET Delegate Recipes</title>
 <link>http://thoughtshapes.com/node/69</link>
 <description>&lt;p&gt;
        Delegates are ubiquitous throughout .NET development though many developers are not aware of their full range. This stems from the fact that few books or articles do a good job tying together all ways in which delegates may be used. Additionally there are often misconceptions over delegate semantics - how to construct a delegate within a given context. This post seeks to clarify the muddy water around delegates by use of demonstrative tests. Each test is explicitly named for the semantic it demonstrates and internally each test asserts expected behavior. As the title implies, this post is not intended as an introduction to delegates. If you are looking for such material Juval Lowy does a decent job &lt;a href=&quot;http://msdn.microsoft.com/msdnmag/issues/06/00/C20/default.aspx&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://thoughtshapes.com/node/69&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://thoughtshapes.com/node/69#comments</comments>
 <category domain="http://thoughtshapes.com/taxonomy/term/11">DotNet</category>
 <enclosure url="http://thoughtshapes.com/files/delegates.zip" length="1323499" type="application/x-zip-compressed" />
 <pubDate>Tue, 30 Oct 2007 23:40:05 -0400</pubDate>
 <dc:creator>Rjae Easton</dc:creator>
 <guid isPermaLink="false">69 at http://thoughtshapes.com</guid>
</item>
</channel>
</rss>
