Anonymous method trivia

by Miha Markič 28. August 2007 15:46

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); } }

Tags:

.net

Comments (5) -

Mladen
Mladen
8/29/2007 12:52:56 AM #

51.6 times exactly! Smile

Reply

Mladen
Mladen
8/29/2007 12:55:16 AM #

without running this i'd say twice.
if it's anything other than that, i have no idea why.

Reply

Bojan
Bojan
8/29/2007 2:17:07 AM #

Jup, twice...i tryed.... Smile

Reply

Mladen
Mladen
8/29/2007 11:06:06 AM #

so miha... what's the catch?
give!!

Reply

Miha Markic
Miha Markic
8/29/2007 11:48:21 AM #

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

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Miha Markic

About me
Righthand
 
Microsoft MVP
 
Developer Express' DXSquad
INETA Country Leader for Slovenia
INETA Country Leader for Slovenia

Slovene Developer Users Group Lead
Friends of Red-Gate
LLBLGenPro Partner

Miha currently works as a free lance consultant and software developer specialized in .net area.
He graduated in Computer and information science at the University of Ljubljana, Slovenia. He has accumulated experience in various programming languages such as Java, Visual Basic 3-6 (MCP), Visual C++, Delphi, C# and VB.Net through years.
He has experience in practically all (technical) stages of project development, including planning, framework development, user interface, business processes, as well as testing and documenting. He has worked on big and small projects in Slovenia and abroad (e.g. participated in completing level 3 IS for the Nucor steel plant, Hertford, USA).
Currently he enjoys programming in .net environment using C#. Since 2000 he has been active in Developer Express' DX Squad and has been ECDL trainer and tester. He also gives lectures on conferences and other events in Slovenia.

Month List

Tag cloud

Most comments

Paulius Paulius
1 comments
us United States
Meh Meh
1 comments
us United States
bart dm bart dm
1 comments
nl Netherlands

RecentComments

Comment RSS