anonymous method

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hi!

Here is a simple example on an anonymous method that works
test.myEvent += delegate(object sender, EventArgs e)
{
Console.WriteLine("Test");
};

How do I write the anonymous method within the EventHandler I have tried
here but this gives compile error
test.myEvent +=new EventHandler(Console.WriteLine("Test"););

//Tony
 
Hi!

Here is a simple example on an anonymous method that works
test.myEvent += delegate(object sender, EventArgs e)
{
Console.WriteLine("Test");
};

How do I write the anonymous method within the EventHandler I have tried
here but this gives compile error
test.myEvent +=new EventHandler(Console.WriteLine("Test"););

//Tony

I prefer your first example, but if you want, you could do this:

test.myEvent += new
EventHandler(delegate {Console.WriteLine("Test");});
 
I prefer your first example, but if you want, you could do this:

test.myEvent += new
EventHandler(delegate {Console.WriteLine("Test");});

You don't even have to do that in 2008:

test.myEvent += (o, ea) => Console.WriteLine("Test");
 
Back
Top