Welcome to RightHand's community place Sign in | | Help

Anonymous method trivia

Can you guess how many times will this console application output the current date?

class Program { private static Tubo tubo = new Tubo(); static void Main(string[] args) { One(); tubo.RaiseTubek(); Console.WriteLine("Finished"); Console.ReadLine(); } private static void One() { tubo.Tubek += delegate { Console.WriteLine(DateTime.Now); }; tubo.RaiseTubek(); } } public class Tubo { public event EventHandler Tubek; public void RaiseTubek() { if (Tubek != null) Tubek(this, EventArgs.Empty); } }
Published 28. avgust 2007 14:46 by Miha Markic
Filed under:

Comments

# re: Anonymous method trivia

28. avgust 2007 15:52 by Mladen
51.6 times exactly! :)

# re: Anonymous method trivia

28. avgust 2007 15:55 by Mladen
without running this i'd say twice. if it's anything other than that, i have no idea why.

# re: Anonymous method trivia

28. avgust 2007 17:17 by Bojan
Jup, twice...i tryed.... :)

# re: Anonymous method trivia

29. avgust 2007 2:06 by Mladen
so miha... what's the catch? give!!

# re: Anonymous method trivia

29. avgust 2007 2:48 by Miha Markic

I am getting there - yes, it is printed twice as you guys guessed (and cheated ;-)). But there is more to come.

Anonymous comments are disabled