Anonymous methods are a condensed way to declare and use delegates
In C# 1.0 and C# 1.1, we typically assign instances of delegates to event properties. For example, in WinForms a Button control exposes a Click event. The delegate type of Click isEventHandler. EventHandler is a method that takes object and EventArgs arguments. Hence, we can initialize an EventHandler object with any method matching the delegate EventHandler's signature and assign that delegate to Click. Here's how the code might look:
private void Form1_Load(object sender, EventArgs e)
{
button1.Click += new EventHandler(OnClick);
}
private void OnClick(object sender, EventArgs e)
{
Debug.WriteLine("button1 clicked");
}
Because the forms designer for WinForms and the page designer for WebForms automatically add the code for delegate binding, it's possible to write a lot of code without binding delegates manually.
Anonymous Methods Are Inline Delegates
Generally, when we're using delegates, we have a method. That method's signature matches the signature prescribed by a delegate and can be used to initialize a delegate instance. Anonymous methods are used to condense the method and initialization of the delegate into a single location.
Using the example from the previous section, we see how the instantiation of the delegate new EventHandler is distinct from the method OnClick used to initialize the delegate. This code could be compressed into an anonymous method:
private void Form1_Load(object sender, EventArgs e)
{
button1.Click += delegate
{
Debug.WriteLine("button1 clicked");
};
}
To create the anonymous method, notice that we removed OnClick's method header and replaced the construction of the EventHandler delegate with the word delegate followed by OnClick's method body. The resultant behavior is the same. If we want to use the event arguments we would normally find associated with the delegate, we can add an optional parameter list after the worddelegate:
private void Form1_Load(object sender, EventArgs e)
{
button1.Click += delegate(object s, EventArgs ev)
{
Debug.WriteLine("object is " + s.ToString());
};
}
If you define delegate parameters, they must match the parameters defined by the delegate type. For example, Click's type is EventHandler, so if arguments are present they must matchEventHandler's arguments object and EventArgs.
Anonymous methods can be used wherever delegates are expected. Anonymous methods can useref and out arguments, but cannot reference ref or out parameters of an outer scope. Anonymous methods can't use unsafe code, and anonymous methods can't use goto, break, orcontinue in such a way that the branch behavior causes a branch outside of the anonymous method's code block.
Summary
Anonymous methods are examples of methods without names that can be defined and used anywhere a delegate can be used. Delegates are wrappers for event handlers. How practical and generally useful anonymous methods are remains to be seen. I suspect that anonymous methods will be much less useful than overloaded operators and used even less frequently, but anonymous methods are part of .NET and it's useful to recognize them when you see them.
OR
Anonymous methods can be used anywhere where a delegate type is expected.Anonymous Method declarations consist of the keyword delegate, an optional parameter list and a statement list enclosed in parenthesis.
Code Snippet: Event Handler (without using anonymous methods)
...
btnSave.Click += new EventHandler (btnSave_Click);
...
void AddClick(object sender, EventArgs e)
{
SaveChanges();
lblStatus.Text = "Your changes have been saved";
}
Code Snippet: Event Handler (using anonymous methods)
btnSave.Click += delegate
Code Snippet: Event Handler using Anonymous Methods, with a parameter list
btnSave.Click += delegate(object sender, EventArgs e)
{
MessageBox.Show(((Button)sender).Text);
SaveChanges();
MessageBox.Show("Your changes have been saved");
}
0 comments:
Post a Comment