|
HTML clipboard
This Lesson will cover the following objectives:
- Understand the benefits of anonymous methods
- Learn how to implement an anonymous method
- Implement anonymous methods that use delegate parameters
How Do Anonymous Methods Benefit Me?
An anonymous method is a method without a name - which is why it is called
anonymous. You don't declare anonymous methods like regular methods. Instead
they get hooked up directly to events. You'll see a code example shortly.
To see the benefit of anonymous methods, you need to look at how they improve
your development experience over using delegates. Think about all of the moving
pieces there are with using delegates: you declare the delegate, write a method
with a signature defined by the delegate interface, delcare the event based on
that delegate, and then write code to hook the handler method up to the
delegate. With all this work to do, no wonder programmers, who are new to C#
delegates, have to do a double-take to understand how they work.
Because you can hook an anonymous method up to an event directly, a couple of
the steps of working with delegates can be removed. The next section shows you
how this works.
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 1 shows how this works.
Listing 1. 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"Hello");
};
Controls.Add(btnHello);
}
}
{
MessageBox.Show(
The code in Listing 1 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. Remember to terminate anonymous methods with a
semi-colon.
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. Listing 2 shows you how to use parameters
if you need to.
Listing 2. 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"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(btnGoodBye);
{
MessageBox.Show(
Controls.Add(btnHello);
}
}
The bold parts of Listing 2 show another Button control added to the
code from Listing 1. 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 thier 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 in Listing
2, 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.
|