I was recently doing some research into Anonymous Methods and began to realize how much coding they could save me in some situations. I have always wished for a shortcut to creating code that responds to events. It has always been a pain to create a method with the (object sender, EventArgs e) signature only to have that method call into something else. Now with Anonymous Methods I don’t have to do that.
So what are Anonymous methods? They are fragments of code that are used with delegates and are written in-line where you would normally reference a method name. The sample below illustrates how this works and compares the old way to the new way.
//old way
addButton.Click += new EventHandler(AddClick);
//new way
addButton.Click += delegate { MessageBox.Show(“Button was clicked”) };
All that you have to do is have the delegate keyword and insert your code between the { } and away you go. There are of course some restrictions on what you can do. The sample below gives you a good idea what you can and can’t do. The form has two buttons normalButton which illustrates your normal event handler and anonymousButton which illustrates an anonymous method.
namespace Anon_Methos_Events
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//standard and would normally be done by VS inside the form1.Designer.cs code
this.normalButton.Click += new System.EventHandler(this.btnNormal_Click);
//(1) This version works due to implicit conversions
this.anonymousButton.Click += delegate
{
MessageBox.Show(“This was done inline with anonymous methods using no parameters and no () after the delegate keyword”);
};
//(2) This version is the same as (1) except calls a method
this.anonymousButton.Click += delegate
{
TestAnonymous.MakeSomethingHappen();
};
//(3) This version works because this parameter signature matches
this.anonymousButton.Click += delegate(object sender, EventArgs e){
MessageBox.Show(“This was done inline with anonymous methods using parameters”);
};
//(4) This won’t work because the empty () indicates that there are no parameters which is incorrect
//this.btnAnonymous.Click += delegate()
//{
// MessageBox.Show(“This was done inline with anonymous methods using no parameters and no () after the delegate keyword”);
//};
}
private void btnNormal_Click(object sender, EventArgs e)
{
MessageBox.Show(“This is a normal event delegate”);
}
}
public static class TestAnonymous
{
public static void MakeSomethingHappen()
{
MessageBox.Show(“Something happened”);
}
}
}
What is going on with (1) and (2) is that an implicit conversion is occurring from the anonymous method to the EventHandler delegate type (the type of the anonymousButtons event). This implicit conversion is possible because the parameter list and return type of the delegate type are compatible with the anonymous method. In (3) the parameter list for the delegate matches what is expected. In (4) by putting the () at the end the implicit conversion can’t happen and therefore the code will not work. For a complete reference for Anonymous Methods visit the Beta2 SDK online at http://msdn2.microsoft.com/library/0yw3tz5k(en-us,vs.80).aspx