anonymous method

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
 
F

Family Tree Mike

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

Tom Shelton

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top