<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>CLR Hoser - AnonymousMethods</title>
    <link>http://hoser.lander.ca/</link>
    <description>(none)</description>
    <language>en-us</language>
    <copyright>Rich Lander</copyright>
    <lastBuildDate>Mon, 05 Dec 2005 19:07:09 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>rich@lander.ca</managingEditor>
    <webMaster>rich@lander.ca</webMaster>
    <item>
      <trackback:ping>http://hoser.lander.ca/Trackback.aspx?guid=8514e6f1-ae11-452b-ad15-c52a445187de</trackback:ping>
      <pingback:server>http://hoser.lander.ca/pingback.aspx</pingback:server>
      <pingback:target>http://hoser.lander.ca/PermaLink,guid,8514e6f1-ae11-452b-ad15-c52a445187de.aspx</pingback:target>
      <dc:creator>Rich Lander</dc:creator>
      <wfw:comment>http://hoser.lander.ca/CommentView,guid,8514e6f1-ae11-452b-ad15-c52a445187de.aspx</wfw:comment>
      <wfw:commentRss>http://hoser.lander.ca/SyndicationService.asmx/GetEntryCommentsRss?guid=8514e6f1-ae11-452b-ad15-c52a445187de</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I'm still on this anonymous delegates kick. Maybe I'm on the wrong team and should
consider a move to the C# team?? 
</p>
        <p>
I've seen references to the feature of anonymous delegates as both "anonymous delegates"
and "anonymous methods". From a philosophical standpoint, both seem like bad names.
It had been bothering me for a while, so I decided to ask my fellow CLR buddy, <a href="http://www.bluebytesoftware.com/blog/">Joe</a>, the
question. Joe is a good person to go to since he's a lot smarter than me. Joe handles
concurrency and threading for our team. Joe was doing such a good job at concurrency
that they decided that he could handle another job ;)
</p>
        <p>
Anyway, Joe agreed that both names are misleading. Here's what we came up with for
what the features would be if you interpreted the names literally:
</p>
        <p>
- anonymous delegates: the ability to define a delegate (the actual <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_15_1.asp">declaration</a>)
in a more dynamic manner, in which you do not need to statically define the signature/contract
of the delegate up-front. <a href="http://blogs.msdn.com/joelpob/">Joel</a> and I
did a quick co-routines implementation on top of the framework last summer. My first
thought was to use delegates. This failed pretty quickly given that you need to delegate
signature to be something better than "public delegate Object[] CoRoutineDelegate(Object[])".
</p>
        <p>
- anonymous methods: the ability to define and generate a method in a more dynamic
manner, in which you do not need to statically define the signature/contact of the
method up-front or care which class it sites on. All you really want here is some
sort of handle/reference to that method that you can pass around and call through
as needed.
</p>
        <p>
The second definition here feels a lot more like what I've been calling anonymous
delegates. In fact, that's more or less how this feature that we've been discussing
actually works. I guess "anonymous methods" is really a better term.
</p>
        <p>
Just for kicks, I decided to make a category on <a href="http://hoser.lander.ca/CategoryView,category,AnonymousMethods.aspx">anonymous
methods</a> so that folks can link back to all the posts, since there are quite a
few now. I'm hoping that this post was the last one on the subject, unless I get some
interesting feedback from the C# team. With that, it's now time to
get back to my <a href="http://hoser.lander.ca/CategoryView,category,Wonders%20of%20Whidbey%20Factoring%20Features.aspx">factoring
features in Whidbey series</a>, which is in dire need of some attention.
</p>
        <img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=8514e6f1-ae11-452b-ad15-c52a445187de" />
      </body>
      <title>Anonymous Delegates vs. Anonymous Methods</title>
      <guid isPermaLink="false">http://hoser.lander.ca/PermaLink,guid,8514e6f1-ae11-452b-ad15-c52a445187de.aspx</guid>
      <link>http://hoser.lander.ca/2005/12/05/AnonymousDelegatesVsAnonymousMethods.aspx</link>
      <pubDate>Mon, 05 Dec 2005 19:07:09 GMT</pubDate>
      <description>&lt;p&gt;
I'm still on this anonymous delegates kick. Maybe I'm on the wrong team and should
consider a move to the C# team?? 
&lt;/p&gt;
&lt;p&gt;
I've seen references to the feature of anonymous delegates as both "anonymous delegates"
and "anonymous methods". From a philosophical standpoint, both seem like bad names.
It had been bothering me for a while, so I decided to ask my fellow CLR buddy,&amp;nbsp;&lt;a href="http://www.bluebytesoftware.com/blog/"&gt;Joe&lt;/a&gt;,&amp;nbsp;the
question. Joe is a good person to go to since he's a lot smarter than me. Joe handles
concurrency and threading for our team. Joe was doing such a good job at concurrency
that they decided that he could handle another job ;)
&lt;/p&gt;
&lt;p&gt;
Anyway, Joe agreed that both names are misleading. Here's what we came up with for
what the features would be if you interpreted the names literally:
&lt;/p&gt;
&lt;p&gt;
- anonymous delegates: the ability to define a delegate (the actual &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_15_1.asp"&gt;declaration&lt;/a&gt;)
in a more dynamic manner, in which you do not need to statically define the signature/contract
of the delegate up-front. &lt;a href="http://blogs.msdn.com/joelpob/"&gt;Joel&lt;/a&gt; and I
did a quick co-routines implementation on top of the framework last summer. My first
thought was to use delegates. This failed pretty quickly given that you need to delegate
signature to be something better than "public delegate Object[] CoRoutineDelegate(Object[])".
&lt;/p&gt;
&lt;p&gt;
- anonymous methods: the ability to define and generate a method in a more dynamic
manner, in which you do not need to statically define the signature/contact of the
method up-front or care which class it sites on. All you really want here is some
sort of handle/reference to that method that you can pass around and call through
as needed.
&lt;/p&gt;
&lt;p&gt;
The second definition here feels a lot more like what I've been calling anonymous
delegates. In fact, that's more or less how this feature that we've been discussing
actually works.&amp;nbsp;I guess "anonymous methods" is really a better term.
&lt;/p&gt;
&lt;p&gt;
Just for kicks, I decided to make a category on &lt;a href="http://hoser.lander.ca/CategoryView,category,AnonymousMethods.aspx"&gt;anonymous
methods&lt;/a&gt; so that folks can link back to all the posts, since there are quite a
few now. I'm hoping that this post was the last one on the subject, unless I get some
interesting&amp;nbsp;feedback from the C#&amp;nbsp;team.&amp;nbsp;With that, it's now time to
get back to my &lt;a href="http://hoser.lander.ca/CategoryView,category,Wonders%20of%20Whidbey%20Factoring%20Features.aspx"&gt;factoring
features in Whidbey series&lt;/a&gt;, which is in dire need of some attention.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=8514e6f1-ae11-452b-ad15-c52a445187de" /&gt;</description>
      <comments>http://hoser.lander.ca/CommentView,guid,8514e6f1-ae11-452b-ad15-c52a445187de.aspx</comments>
      <category>AnonymousMethods</category>
    </item>
    <item>
      <trackback:ping>http://hoser.lander.ca/Trackback.aspx?guid=1ae8f9b3-766b-49cc-a3dd-f7aa4cf3bcfa</trackback:ping>
      <pingback:server>http://hoser.lander.ca/pingback.aspx</pingback:server>
      <pingback:target>http://hoser.lander.ca/PermaLink,guid,1ae8f9b3-766b-49cc-a3dd-f7aa4cf3bcfa.aspx</pingback:target>
      <dc:creator>Rich Lander</dc:creator>
      <wfw:comment>http://hoser.lander.ca/CommentView,guid,1ae8f9b3-766b-49cc-a3dd-f7aa4cf3bcfa.aspx</wfw:comment>
      <wfw:commentRss>http://hoser.lander.ca/SyndicationService.asmx/GetEntryCommentsRss?guid=1ae8f9b3-766b-49cc-a3dd-f7aa4cf3bcfa</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <title>Anonymous Delegates in C# v2.0 -- Are They Lexical Closures?</title>
      <guid isPermaLink="false">http://hoser.lander.ca/PermaLink,guid,1ae8f9b3-766b-49cc-a3dd-f7aa4cf3bcfa.aspx</guid>
      <link>http://hoser.lander.ca/2005/12/03/AnonymousDelegatesInCV20AreTheyLexicalClosures.aspx</link>
      <pubDate>Sat, 03 Dec 2005 05:40:06 GMT</pubDate>
      <description>&lt;p&gt;
&lt;font color=#000000&gt;I recently posted twice (&lt;/font&gt;&lt;a href="http://hoser.lander.ca/PermaLink,guid,20c75894-5947-4a62-a9c6-01b14516ecf8.aspx"&gt;&lt;font color=#000000&gt;one&lt;/font&gt;&lt;/a&gt;&lt;font color=#000000&gt; and &lt;/font&gt;&lt;a href="http://hoser.lander.ca/PermaLink,guid,3139853d-eff9-41ff-989f-4f6697946453.aspx"&gt;&lt;font color=#000000&gt;two&lt;/font&gt;&lt;/a&gt;&lt;font color=#000000&gt;)
on anonymous delegates. I guess they really caught my attention for some reason, even
though I've known about them for a long time. The remaining question to close out
this debate is whether anonymous delegates are the .Net Framework answer to closures.
Another related topic is continuations. I'm certain that these are not that.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;First, what is a closure? &lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;Here is how wikipedia defines &lt;/font&gt;&lt;a href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29"&gt;&lt;font color=#000000&gt;closures&lt;/font&gt;&lt;/a&gt;&lt;font color=#000000&gt;:&amp;nbsp;
"Unlike garden-variety functions which retain no memory of what happened in previous
calls, &lt;b&gt;closures are capable of storing information across function calls&lt;/b&gt;."&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Verdana"&gt;&lt;font color=#000000&gt;Here
is another &lt;/font&gt;&lt;a href="http://www.gnu.org/software/guile/docs/guile-ref/About-Closure.html"&gt;&lt;font color=#000000&gt;explanation&lt;/font&gt;&lt;/a&gt;&lt;font color=#000000&gt;&amp;nbsp;from &lt;/font&gt;&lt;a href="http://www.gnu.org/software/guile/"&gt;&lt;font color=#000000&gt;guile&lt;/font&gt;&lt;/a&gt;&lt;font color=#000000&gt;,
which is an open source&amp;nbsp;scheme interpreter that I've never heard of before: "The
concept of &lt;dfn&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;closure&lt;/span&gt;&lt;/dfn&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt; is
the idea that a lambda expression "captures" the variable bindings that are in lexical
scope at the point where the lambda expression occurs. The procedure created by the
lambda expression can refer to and mutate the captured bindings, and the values of
those bindings persist between procedure calls. "&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Verdana"&gt;&lt;font color=#000000&gt;My
definition is (as of 5 mins ago): a &lt;em&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;closure&lt;/span&gt;&lt;/em&gt; is
the outcome of capturing both the in-scope variables used by a&amp;nbsp;block of code&amp;nbsp;and
the block of code itself to the end of preserving their relationship beyond the normal
lifetime of those in-scope local variables. As a result, the local variables can be
shared across all calls to the block of code (i.e. method).&amp;nbsp; &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;&lt;/font&gt; 
&lt;p&gt;
&lt;font color=#000000&gt;So,&amp;nbsp;do anonymous delegates&amp;nbsp;in C# v2.0 conform to the&amp;nbsp;definition
of&amp;nbsp;lexical closures? That seems to be a matter of great debate. It is amazing
when you start to look at a topic and then you realize that there has been a whole
crowd to the party before you. Anonymous delegates in C# appear to be just such a
topic. &lt;/font&gt;&lt;a href="http://blogs.msdn.com/brada/default.aspx"&gt;&lt;font color=#000000&gt;Brad&lt;/font&gt;&lt;/a&gt;&lt;font color=#000000&gt; appears
to have been interested in the topic &lt;/font&gt;&lt;a href="http://blogs.msdn.com/brada/archive/2004/08/03/207164.aspx"&gt;&lt;font color=#000000&gt;about
a year ago&lt;/font&gt;&lt;/a&gt;&lt;font color=#000000&gt;. Brad takes the stance (at least at that
time) that anonymous delegates &lt;/font&gt;&lt;a href="http://blogs.msdn.com/brada/archive/2004/08/03/207164.aspx#209296"&gt;&lt;font color=#000000&gt;are
not closures&lt;/font&gt;&lt;/a&gt;&lt;font color=#000000&gt;. &lt;/font&gt;&lt;a href="http://blogs.msdn.com/abhinaba"&gt;&lt;font color=#000000&gt;abhinaba&lt;/font&gt;&lt;/a&gt;&lt;font color=#000000&gt; also
takes the same &lt;/font&gt;&lt;a href="http://blogs.msdn.com/abhinaba/archive/2005/08/08/448939.aspx"&gt;&lt;font color=#000000&gt;stance&lt;/font&gt;&lt;/a&gt;&lt;font color=#000000&gt;. &lt;/font&gt;&lt;a href="http://c2.com/cgi/wiki?DanMuller"&gt;&lt;font color=#000000&gt;Dan
Muller&lt;/font&gt;&lt;/a&gt;&lt;font color=#000000&gt; takes the opposite opinion, believing that C# &lt;/font&gt;&lt;a href="http://c2.com/cgi/wiki?LexicalClosure"&gt;&lt;font color=#000000&gt;has
implemented closures&lt;/font&gt;&lt;/a&gt;&lt;font color=#000000&gt;.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;I've come to the conclusion that anonymous delegates are closures.
If you look at my somewhat-in-depth explanation of anonymous delegates in my &lt;/font&gt;&lt;a href="http://hoser.lander.ca/PermaLink,guid,3139853d-eff9-41ff-989f-4f6697946453.aspx"&gt;&lt;font color=#000000&gt;last&amp;nbsp;post&lt;/font&gt;&lt;/a&gt;&lt;font color=#000000&gt;,
you'll see that the way they work seems to conform to the three definitions above.
In summary, the C# compiler generates a class on your behalf which contains both an
instance method (which is your anonymous delegate) and fields that map directly to
the in-scope variables that you access in the containing method. All access to these
variables are wired up to these generated fields on the class. That generated class
is in itself the closure. Being heap-allocated, it naturally can have a different
lifetime than what the containing method has on the stack, given that stack-frames
have a tendency to go way.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;I have a mail out to the C# team asking them their opinion on
this interesting subject. I'm curious to see how they'll respond.&lt;/font&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=1ae8f9b3-766b-49cc-a3dd-f7aa4cf3bcfa" /&gt;</description>
      <comments>http://hoser.lander.ca/CommentView,guid,1ae8f9b3-766b-49cc-a3dd-f7aa4cf3bcfa.aspx</comments>
      <category>AnonymousMethods</category>
    </item>
    <item>
      <trackback:ping>http://hoser.lander.ca/Trackback.aspx?guid=d5cf6962-ad80-4a69-893a-5c1a6f5f3c44</trackback:ping>
      <pingback:server>http://hoser.lander.ca/pingback.aspx</pingback:server>
      <pingback:target>http://hoser.lander.ca/PermaLink,guid,d5cf6962-ad80-4a69-893a-5c1a6f5f3c44.aspx</pingback:target>
      <dc:creator>Rich Lander</dc:creator>
      <wfw:comment>http://hoser.lander.ca/CommentView,guid,d5cf6962-ad80-4a69-893a-5c1a6f5f3c44.aspx</wfw:comment>
      <wfw:commentRss>http://hoser.lander.ca/SyndicationService.asmx/GetEntryCommentsRss?guid=d5cf6962-ad80-4a69-893a-5c1a6f5f3c44</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Here is a better example of using an anonymous delegate that proves, in my mind, that
they are lexical closures. I sure hope <a href="http://hoser.lander.ca/PermaLink,guid,1ae8f9b3-766b-49cc-a3dd-f7aa4cf3bcfa.aspx">my
definition </a>of closures is correct ;)
</p>
        <p>
Here's the code. Please do guess what it does or better yet, compile and run it. The
important aspect is to guess which values of "s" do or do not match.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System;<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Threading;<br /><br /><br /><br /><br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> AnonDelegateTest2<br /><br />
{<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Program<br /><br />
{<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">delegate</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> FunkyDelegate();<br /><br /><br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Main(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span>[]
args)<br /><br />
{<br /><br />
FunkyDelegate fd <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> GetDelegate();<br /><br /><br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">for</span> (Int32
i <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 0;
i &lt; 10; i++)<br /><br />
{<br /><br />
fd();<br /><br />
}<br /><br />
}<br /><br /><br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span> FunkyDelegate
GetDelegate()<br /><br />
{<br /><br />
String s <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> DateTime.Now.Ticks.ToString();<br /><br /><br /><br />
FunkyDelegate fd <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">delegate</span>()<br /><br />
{<br /><br />
Console.WriteLine(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Entering
fd"</span>);<br /><br />
Console.WriteLine(s);<br /><br />
s <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> DateTime.Now.Ticks.ToString();<br /><br />
Thread.Sleep(100);<br /><br />
Console.WriteLine(s);<br /><br />
Console.WriteLine(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Leaving
fd"</span>);<br /><br />
Console.WriteLine();<br /><br />
};<br /><br /><br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> fd;<br /><br />
}<br /><br />
}<br /><br /><br /><br />
}<br /></span>
        </pre>
        <img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=d5cf6962-ad80-4a69-893a-5c1a6f5f3c44" />
      </body>
      <title>Another Anonymous Delegate Example </title>
      <guid isPermaLink="false">http://hoser.lander.ca/PermaLink,guid,d5cf6962-ad80-4a69-893a-5c1a6f5f3c44.aspx</guid>
      <link>http://hoser.lander.ca/2005/12/03/AnotherAnonymousDelegateExample.aspx</link>
      <pubDate>Sat, 03 Dec 2005 02:06:27 GMT</pubDate>
      <description>&lt;p&gt;
Here is a better example of using an anonymous delegate that proves, in my mind,&amp;nbsp;that
they are lexical closures. I sure hope &lt;a href="http://hoser.lander.ca/PermaLink,guid,1ae8f9b3-766b-49cc-a3dd-f7aa4cf3bcfa.aspx"&gt;my
definition &lt;/a&gt;of closures is correct ;)
&lt;/p&gt;
&lt;p&gt;
Here's the code. Please do guess what it does or better yet, compile and run it. The
important aspect is to guess which values of "s" do or do not match.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System;&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Threading;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; AnonDelegateTest2&lt;br&gt;
&lt;br&gt;
{&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Program&lt;br&gt;
&lt;br&gt;
{&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;delegate&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; FunkyDelegate();&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Main(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt;[]
args)&lt;br&gt;
&lt;br&gt;
{&lt;br&gt;
&lt;br&gt;
FunkyDelegate fd &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; GetDelegate();&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;for&lt;/span&gt; (Int32
i &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 0;
i &amp;lt; 10; i++)&lt;br&gt;
&lt;br&gt;
{&lt;br&gt;
&lt;br&gt;
fd();&lt;br&gt;
&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; FunkyDelegate
GetDelegate()&lt;br&gt;
&lt;br&gt;
{&lt;br&gt;
&lt;br&gt;
String s &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; DateTime.Now.Ticks.ToString();&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
FunkyDelegate fd &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;delegate&lt;/span&gt;()&lt;br&gt;
&lt;br&gt;
{&lt;br&gt;
&lt;br&gt;
Console.WriteLine(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Entering
fd"&lt;/span&gt;);&lt;br&gt;
&lt;br&gt;
Console.WriteLine(s);&lt;br&gt;
&lt;br&gt;
s &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; DateTime.Now.Ticks.ToString();&lt;br&gt;
&lt;br&gt;
Thread.Sleep(100);&lt;br&gt;
&lt;br&gt;
Console.WriteLine(s);&lt;br&gt;
&lt;br&gt;
Console.WriteLine(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Leaving
fd"&lt;/span&gt;);&lt;br&gt;
&lt;br&gt;
Console.WriteLine();&lt;br&gt;
&lt;br&gt;
};&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; fd;&lt;br&gt;
&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=d5cf6962-ad80-4a69-893a-5c1a6f5f3c44" /&gt;</description>
      <comments>http://hoser.lander.ca/CommentView,guid,d5cf6962-ad80-4a69-893a-5c1a6f5f3c44.aspx</comments>
      <category>AnonymousMethods</category>
      <category>Code Samples</category>
    </item>
    <item>
      <trackback:ping>http://hoser.lander.ca/Trackback.aspx?guid=3139853d-eff9-41ff-989f-4f6697946453</trackback:ping>
      <pingback:server>http://hoser.lander.ca/pingback.aspx</pingback:server>
      <pingback:target>http://hoser.lander.ca/PermaLink,guid,3139853d-eff9-41ff-989f-4f6697946453.aspx</pingback:target>
      <dc:creator>Rich Lander</dc:creator>
      <wfw:comment>http://hoser.lander.ca/CommentView,guid,3139853d-eff9-41ff-989f-4f6697946453.aspx</wfw:comment>
      <wfw:commentRss>http://hoser.lander.ca/SyndicationService.asmx/GetEntryCommentsRss?guid=3139853d-eff9-41ff-989f-4f6697946453</wfw:commentRss>
      <slash:comments>5</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I <a href="http://hoser.lander.ca/PermaLink,guid,20c75894-5947-4a62-a9c6-01b14516ecf8.aspx">posted
an interesting bit</a> of C# a couple weeks ago relating to anonymous delegates. I
asked in that post what the result of the following code is. Here is the code and
then the result
</p>
        <p>
          <strong>Code:<br /></strong>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System;<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Threading;<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> AnonMethods<br />
{<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Program<br />
    {<br />
        <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Main(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span>[]
args)<br />
        {<br />
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">for</span> (Int32
i <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 0;
i &lt; 20; i++)<br />
            {<br />
               
Thread t <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Thread(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">delegate</span>()
{ Thread.Sleep(10); Console.WriteLine(i); });<br />
               
t.Start();<br />
            }<br />
            Thread.Sleep(5000);<br />
        }<br />
    }<br />
}<br /></span>
        </p>
        <p>
          <strong>Result:<br /></strong>6<br />
6<br />
6<br />
6<br />
6<br />
9<br />
12<br />
12<br />
12<br />
13<br />
13<br />
13<br />
15<br />
15<br />
19<br />
20<br />
20<br />
20<br />
20<br />
20
</p>
        <p>
          <font color="#000000">
            <strong>Why?<br /></strong>That's the strangest result from a for loop that I've ever seen. First, I'd
suggest that you compile the code for yourself (remember, <a href="http://msdn.microsoft.com/vstudio/express/visualCsharp/default.aspx">C#
Express</a> is free) and then take and then decompile it in either ildasm or <a href="http://www.aisto.com/roeder/dotnet/">Reflector</a>.
I find the C# in Reflector a whole lot easier to read than the IL, so I'd recommend
that you start with it. I imagine that you'll get a real kick out of what you see.</font>
        </p>
        <p>
          <font color="#000000">First, I'd like to caveat my explanation by saying that this
is a CLR guy's explanation of what the C# compiler is doing. I haven't talked to the
C# team, but just took a look at the code emitted by the compiler, which both
you and I are free to ponder. All that being said, here's the basic idea
of what I think is going  on without going into great detail of the mechanics
of anonymous delegates. </font>
        </p>
        <p>
          <font color="#000000">Once the C# source code is compiled, the anonymous delegate
goes away and is replaced by the mechanism that I'm about to describe. Put another
way, anonymous delegates are a C# source-code-only feature. For every anonymous delegate,
there is a strangely, but probably predictably, named nested class added to the
class that indirectly contained (within one of the methods within the class) the
anonymous delegate. The class, as you might expect, exposes some surface area
that is accessed from the parent class. The class is actually private, which means
it cannot be accessed from anywhere but the parent class. There is a default
constructor, which does nothing. There is a method that is the anonymous delegate
transformed into a regular named method. Remember, anonymous delegates are a C#, not
a CLR feature, so the CLR does need a named method to call. Lastly, there are a set
of fields exposed on this compiler-generated class -- and this is the part that gets
interesting -- that are the local variables that are accessed from
both the containing method and the anonymous delegate. Variables that are defined
in the containing method but not accessed from within the anonymous delegate or that
are defined and used within the anonymous delegate do not get this special treatment.
As you might guess, the C# compiler wires up the method that takes the delegate and
the generated named method. In addition, all accesses to the shared local variables
are wired up through the public fields on the generated class instead of through the
actual locals. Very interesting.</font>
        </p>
        <p>
          <font color="#000000">OK, so that's the basic explananation of how anonymous delegates
work. There are some other subtleties that I noticed, but they are not important for
this explanation. I'm going to get further insight on those from the C# team and will
post on those when I learn more about them.</font>
        </p>
        <p>
          <font color="#000000">So, then, how does all that lead to the strange and unexpected
results from the for loop. Well, since the "i" variable is accessed by both the containing
method and the anonymous delegate, the C# compiler rewires all accesses to "i" to
the "i" field on the generated class. And since each call to the anonymous delegate
in our example does its work on a separate thread, then "i" is updated by the for
loop on a different schedule than it is written by Console.WriteLine in the anonymous
delegate. That's why the value printed out by each call to the anonymous
delegate is essentially a race condition between the for loop continually iterating
and threads being </font>
          <font color="#000000">created and getting time on the processor(s).</font>
        </p>
        <p>
          <font color="#000000">Another aspect of this is that shared locals with
anonymous delegates essentially turn value types into reference types. That's
a really strange behaviour and realization.</font>
        </p>
        <p>
          <font color="#000000">Like I said in the earlier post, the trick is determining a
scenario where this behaviour is useful. I haven't found that yet, although I'm sure
it will be very interesting once I find it.</font>
        </p>
        <img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=3139853d-eff9-41ff-989f-4f6697946453" />
      </body>
      <title>Anonymous Delegates -- Explained</title>
      <guid isPermaLink="false">http://hoser.lander.ca/PermaLink,guid,3139853d-eff9-41ff-989f-4f6697946453.aspx</guid>
      <link>http://hoser.lander.ca/2005/11/30/AnonymousDelegatesExplained.aspx</link>
      <pubDate>Wed, 30 Nov 2005 15:51:11 GMT</pubDate>
      <description>&lt;p&gt;
I &lt;a href="http://hoser.lander.ca/PermaLink,guid,20c75894-5947-4a62-a9c6-01b14516ecf8.aspx"&gt;posted
an interesting bit&lt;/a&gt; of C# a couple weeks ago relating to anonymous delegates. I
asked in that post what the result of the following code is. Here is the code and
then the result
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Code:&lt;br&gt;
&lt;/strong&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System;&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Threading;&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; AnonMethods&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Program&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Main(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt;[]
args)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;for&lt;/span&gt; (Int32
i &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 0;
i &amp;lt; 20; i++)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
Thread t &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Thread(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;delegate&lt;/span&gt;()
{ Thread.Sleep(10); Console.WriteLine(i); });&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
t.Start();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Thread.Sleep(5000);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Result:&lt;br&gt;
&lt;/strong&gt;6&lt;br&gt;
6&lt;br&gt;
6&lt;br&gt;
6&lt;br&gt;
6&lt;br&gt;
9&lt;br&gt;
12&lt;br&gt;
12&lt;br&gt;
12&lt;br&gt;
13&lt;br&gt;
13&lt;br&gt;
13&lt;br&gt;
15&lt;br&gt;
15&lt;br&gt;
19&lt;br&gt;
20&lt;br&gt;
20&lt;br&gt;
20&lt;br&gt;
20&lt;br&gt;
20
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;&lt;strong&gt;Why?&lt;br&gt;
&lt;/strong&gt;That's the strangest result from a for loop that I've ever seen. First, I'd
suggest that you compile the code for yourself (remember, &lt;a href="http://msdn.microsoft.com/vstudio/express/visualCsharp/default.aspx"&gt;C#
Express&lt;/a&gt; is free) and then take and then decompile it in either ildasm or &lt;a href="http://www.aisto.com/roeder/dotnet/"&gt;Reflector&lt;/a&gt;.
I find the C# in Reflector a whole lot easier to read than the IL, so I'd recommend
that you start with it. I imagine that you'll get a real kick out of what you see.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;First, I'd like to caveat my explanation by saying that this is
a CLR guy's explanation of what the C# compiler is doing. I haven't talked to the
C# team, but&amp;nbsp;just took a look at the code emitted by the compiler, which both
you and I are free to&amp;nbsp;ponder. All that being said,&amp;nbsp;here's the basic idea
of what I think is going&amp;nbsp; on without going into great detail of the mechanics
of anonymous delegates. &lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;Once the C# source code is&amp;nbsp;compiled, the anonymous delegate
goes away and is replaced by the mechanism that I'm about to describe. Put another
way, anonymous delegates are a C# source-code-only feature. For every anonymous delegate,
there is a strangely, but probably predictably, named&amp;nbsp;nested class added to the
class that indirectly contained (within one of the methods within the class)&amp;nbsp;the
anonymous delegate.&amp;nbsp;The class, as you might expect, exposes some surface area
that is accessed from the parent class. The class is actually private, which means
it cannot be accessed from anywhere but the parent class.&amp;nbsp;There is a default
constructor, which does nothing. There is a method that is the anonymous delegate
transformed into a regular named method. Remember, anonymous delegates are a C#, not
a CLR feature, so the CLR does need a named method to call. Lastly, there are a set
of fields exposed on this compiler-generated class -- and this is the part that gets
interesting -- that are the&amp;nbsp;local&amp;nbsp;variables&amp;nbsp;that are accessed from
both the containing method and the anonymous delegate. Variables that are defined
in the containing method but not accessed from within the anonymous delegate or that
are defined and used within the anonymous delegate do not get this special treatment.
As you might guess, the C# compiler wires up the method that takes the delegate and
the generated named method. In addition, all accesses to the shared local variables
are wired up through the public fields on the generated class instead of through the
actual locals. Very interesting.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;OK, so that's the basic explananation of how anonymous delegates
work. There are some other subtleties that I noticed, but they are not important for
this explanation. I'm going to get further insight on those from the C# team and will
post on those when I learn more about them.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;So, then, how does all that lead to the strange and unexpected
results from the for loop. Well, since the "i" variable is accessed by both the containing
method and the anonymous delegate, the C# compiler rewires all accesses to "i" to
the "i" field on the generated class. And since each call to the anonymous delegate
in our example does its work on a separate thread, then "i" is updated by the for
loop on a different schedule than it is written by Console.WriteLine in the anonymous
delegate. That's&amp;nbsp;why the value printed&amp;nbsp;out by each call to the anonymous
delegate is essentially a race condition between the for loop continually iterating
and threads being &lt;/font&gt;&lt;font color=#000000&gt;created and getting time on the processor(s).&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;Another aspect of this is that&amp;nbsp;shared locals&amp;nbsp;with anonymous
delegates essentially turn value types into reference types.&amp;nbsp;That's a really
strange behaviour and realization.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;Like I said in the earlier post, the trick is determining a scenario
where this behaviour is useful. I haven't found that yet, although I'm sure it will
be very interesting once I find it.&lt;/font&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=3139853d-eff9-41ff-989f-4f6697946453" /&gt;</description>
      <comments>http://hoser.lander.ca/CommentView,guid,3139853d-eff9-41ff-989f-4f6697946453.aspx</comments>
      <category>AnonymousMethods</category>
      <category>Code Samples</category>
    </item>
    <item>
      <trackback:ping>http://hoser.lander.ca/Trackback.aspx?guid=20c75894-5947-4a62-a9c6-01b14516ecf8</trackback:ping>
      <pingback:server>http://hoser.lander.ca/pingback.aspx</pingback:server>
      <pingback:target>http://hoser.lander.ca/PermaLink,guid,20c75894-5947-4a62-a9c6-01b14516ecf8.aspx</pingback:target>
      <dc:creator>Rich Lander</dc:creator>
      <wfw:comment>http://hoser.lander.ca/CommentView,guid,20c75894-5947-4a62-a9c6-01b14516ecf8.aspx</wfw:comment>
      <wfw:commentRss>http://hoser.lander.ca/SyndicationService.asmx/GetEntryCommentsRss?guid=20c75894-5947-4a62-a9c6-01b14516ecf8</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
What does the following C# code do? Guess and then run it.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System;<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Threading;<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> AnonMethods<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Program<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Main(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span>[]
args)<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">for</span> (Int32
i <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 0;
i &lt; 20; i++)<br />
{<br />
Thread t <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Thread(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">delegate</span>()
{ Thread.Sleep(10); Console.WriteLine(i); });<br />
t.Start();<br />
}<br />
Thread.Sleep(5000);<br />
}<br />
}<br />
}<br /></span>
        </pre>
        <p>
We were playing with this and similar code in the experts area at the Ottawa VS launch
yesterday. Fun, fun.
</p>
        <p>
My task ahead is to find a scenario that makes the subtleties of anonymous delegates
useful. Anonymous delegates are certainly useful. It is the trick above with "i" that
I'm talking about. I'll post in a couple days with more data on what is going on.
</p>
        <img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=20c75894-5947-4a62-a9c6-01b14516ecf8" />
      </body>
      <title>Anonymous Delegates</title>
      <guid isPermaLink="false">http://hoser.lander.ca/PermaLink,guid,20c75894-5947-4a62-a9c6-01b14516ecf8.aspx</guid>
      <link>http://hoser.lander.ca/2005/11/11/AnonymousDelegates.aspx</link>
      <pubDate>Fri, 11 Nov 2005 14:51:50 GMT</pubDate>
      <description>&lt;p&gt;
What does the following C# code do? Guess and then run it.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System;&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Threading;&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; AnonMethods&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Program&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Main(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt;[]
args)&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;for&lt;/span&gt; (Int32
i &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 0;
i &amp;lt; 20; i++)&lt;br&gt;
{&lt;br&gt;
Thread t &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Thread(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;delegate&lt;/span&gt;()
{ Thread.Sleep(10); Console.WriteLine(i); });&lt;br&gt;
t.Start();&lt;br&gt;
}&lt;br&gt;
Thread.Sleep(5000);&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
We were playing with this and similar code in the experts area at the Ottawa VS launch
yesterday. Fun, fun.
&lt;/p&gt;
&lt;p&gt;
My task ahead is to find a scenario that makes the subtleties of anonymous delegates
useful. Anonymous delegates are certainly useful. It is the trick above with "i" that
I'm talking about. I'll post in a couple days with more data on what is going on.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=20c75894-5947-4a62-a9c6-01b14516ecf8" /&gt;</description>
      <comments>http://hoser.lander.ca/CommentView,guid,20c75894-5947-4a62-a9c6-01b14516ecf8.aspx</comments>
      <category>AnonymousMethods</category>
      <category>Code Samples</category>
    </item>
  </channel>
</rss>