# Friday, November 11, 2005
« Ottawa VS 2005 Launch | Main | Gacutil v2.0 »

What does the following C# code do? Guess and then run it.

using System;
using System.Threading;

namespace AnonMethods
{
class Program
{
static void Main(string[] args)
{
for (Int32 i = 0; i < 20; i++)
{
Thread t = new Thread(delegate() { Thread.Sleep(10); Console.WriteLine(i); });
t.Start();
}
Thread.Sleep(5000);
}
}
}

We were playing with this and similar code in the experts area at the Ottawa VS launch yesterday. Fun, fun.

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.

Friday, November 11, 2005 2:51:50 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [1]  |  Related posts:
Determining Number of Hardlinks for a File
Visual Studio SP1 on Windows Vista Experience
Getting the list of loaded assemblies from another App domain
How to avoid assembly loads
Getting the list of loaded assemblies
FileVersion Sample
Tracked by:
"Anonymous Delegates -- Explained" (CLR Hoser) [Trackback]
http://hoser.lander.ca/PermaLink,guid,3139853d-eff9-41ff-989f-4f6697946453.aspx [Pingback]
"Anonymous Delegates in C# v2.0 -- Are They Lexical Closures?" (CLR Hoser) [Trackback]
http://hoser.lander.ca/PermaLink,guid,1ae8f9b3-766b-49cc-a3dd-f7aa4cf3bcfa.aspx [Pingback]
"Anonymous Delegates in C# v2.0 -- Are They Lexical Closures?" (CLR Hoser) [Trackback]
http://hoser.lander.ca/AnonymousDelegatesInCV20AreTheyLexicalClosures.aspx [Pingback]
"Anonymous Delegates -- Explained" (CLR Hoser) [Trackback]
http://hoser.lander.ca/AnonymousDelegatesExplained.aspx [Pingback]
"gay sauna montreal" (online) [Trackback]
"patio shade cloth" (online) [Trackback]
"bad debt management" (online) [Trackback]
"ktm motorcycle" (online) [Trackback]
"allison lange" (online) [Trackback]
"And some else, come here..." (FlasheR!) [Trackback]
"And some else, come here..." (FlasheR!) [Trackback]
"wheel balancer" (title) [Trackback]
"mustafa amar" (title) [Trackback]
"reju" (title) [Trackback]
"whenever beforehand" (become we're) [Trackback]
"have men dog fucker was" (changes men dog fucker hello) [Trackback]
"i.erasq.info" (i.erasq.info) [Trackback]
"05f.forsi.info" (05f.forsi.info) [Trackback]
"fa3.forsi.info" (fa3.forsi.info) [Trackback]
"ww" (ww) [Trackback]
"826.eiri2.cn" (826.eiri2.cn) [Trackback]
"0cb.dopazas" (0cb.dopazas) [Trackback]
"09c.fiocopa" (09c.fiocopa) [Trackback]
"b82.xierty" (b82.xierty) [Trackback]
"ebb.losare.cn" (ebb.losare.cn) [Trackback]
"190.jopas.cn" (190.jopas.cn) [Trackback]
"bd3.toiera.cn" (bd3.toiera.cn) [Trackback]
"d35.acilot.cn" (d35.acilot.cn) [Trackback]
"dd9.vilase.cn" (dd9.vilase.cn) [Trackback]
"6ea.liasdop.cn" (6ea.liasdop.cn) [Trackback]
"8ea.nuna5k.cn" (8ea.nuna5k.cn) [Trackback]
"025.zopaser.cn" (025.zopaser.cn) [Trackback]
"0d9.kilo4me.cn" (0d9.kilo4me.cn) [Trackback]

Monday, November 14, 2005 10:00:52 PM (GMT Standard Time, UTC+00:00)
Does this imply that anonymous delegates are like ruby closures, in that it is refering to a variable that was in scope when the delegate was defined?
Steve Webb
Comments are closed.