Anonymous Method in C
Implementing an Anonymous Method
An anonymous method uses the keyword, delegate, instead of a method name. This is followed by the body of the method. Typical usage of an anonymous method is to assign it to an event. Listing 21-1 shows how this works.
Implementing an Anonymous Method
using System.Windows.Forms;
public partial class Form1 : Form
{
public Form1()
{
Button btnHello = new Button();
btnHello.Text = "Hello";
btnHello.Click +=
delegate
{
MessageBox.Show("Hello");
};
Controls.Add(btnHello);
}
}
The code above is a Windows Forms application. It instantiates a Button control and sets its Text to "Hello". Notice the combine, +=, syntax being used to hook up the anonymous method. You can tell that it is an anonymous method because it uses the delegate keyword, followed by the method body in curly braces.
Essentially, you have defined a method inside of a method, but the body of the anonymous method doesn't execute with the rest of the code. Because you hook it up to the event, the anonymous method doesn't execute until the Click event is raised. When you run the program and click the Hello button, you'll see a message box that say's "Hello" - courtesy of the anonymous method.
Using Controls.Add, adds the new button control to the window. Otherwise the window wouldn't know anything about the Button and you wouldn't see the button when the program runs.
Using Delegate Parameters with Anonymous Methods
Many event handlers need to use the parameters of the delegate they are based on. The previous example didn't use those parameters, so it was more convenient to not declare them, which C# allows.
Using Parameters with Anonymous Methods
using System;
using System.Windows.Forms;
public partial class Form1 : Form
{
public Form1()
{
Button btnHello = new Button();
btnHello.Text = "Hello";
btnHello.Click +=
delegate
{
MessageBox.Show("Hello");
};
Button btnGoodBye = new Button();
btnGoodBye.Text = "Goodbye";
btnGoodBye.Left = btnHello.Width + 5;
btnGoodBye.Click +=
delegate(object sender, EventArgs e)
{
string message = (sender as Button).Text;
MessageBox.Show(message);
};
Controls.Add(btnHello);
Controls.Add(btnGoodBye);
}
}
The bold parts of show another Button control added to the code. Besides changing the text, btnGoodBye is moved to the right of btnHello by setting it's Left property to 5 pixels beyond the right edge of btnHello. If we didn't do this, btnGoodBye would cover btnHello because both of their Top and Left properties would default to 0.
Beyond implementation details, the real code for you to pay attention to is the implementation of the anonymous method. Notice that the delegate keyword now has a parameter list. this parameter list must match the delegate type of the event that the anonymous method is being hooked up to. The delegate type of the Click event is EventHandler, which has the following signature: public delegate void EventHandler(object sender, EventArgs e);
Notice the EventHandler parameters. Now, here's how the Button control's Click event is defined: public event EventHandler Click;
Notice that the delegate type of the Click event is EventHandler. This is why the anonymous method, assigned to btnGoodBye.Click, must have the same parameters as the EventHandler delegate.
Summary
Anonymous methods are a simplified way for you to assign handlers to events. They take less effort than delegates and are closer to the event they are associated with. You have the choice of either declaring the anonymous method with no parameters or you can declare the parameters if you need them.
Services: - Anonymous Method in C Homework | Anonymous Method in C Homework Help | Anonymous Method in C Homework Help Services | Live Anonymous Method in C Homework Help | Anonymous Method in C Homework Tutors | Online Anonymous Method in C Homework Help | Anonymous Method in C Tutors | Online Anonymous Method in C Tutors | Anonymous Method in C Homework Services | Anonymous Method in C