German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


Anonymous methods in C# 2.0

Written by omerkamal on Mar 10, 2007
little discusion about Anonymous methods. A new standard for C# 2.0

Explanation:

Event handlers and other callbacks are often invoked exclusively through delegates and never directly. Even so, it has thus far been necessary to place the code of event handlers and callbacks in distinct methods to which delegates are explictly created. In contrast, anonymous methods allow the code associated with a delegate to be written “in-line” where the delegate is used, conveniently tying the code directly to the delegate instance. Besides this convenience, anonymous methods have shared access to the local state of the containing function member. To achieve the same state sharing using named methods requires “lifting” local variables into fields in instances of manually authored helper classes.
The following example shows a simple input form that contains a list box, a text box, and a button. When the button is clicked, an item containing the text in the text box is added to the list box.
class InputForm: Form
{
      ListBox listBox;
      TextBox textBox;
      Button addButton;
      public MyForm() {
            listBox = new ListBox(...);
            textBox = new TextBox(...);
            addButton = new Button(...);
            addButton.Click += new EventHandler(AddClick);
      }
      void AddClick(object sender, EventArgs e) {
            listBox.Items.Add(textBox.Text);
      }
}
Even though only a single statement is executed in response to the button’s Click event, that statement must be extracted into a separate method with a full parameter list, and an EventHandler delegate referencing that method must be manually created. Using an anonymous method, the event handling code becomes significantly more succinct:
class InputForm: Form
{
      ListBox listBox;
      TextBox textBox;
      Button addButton;
      public MyForm() {
            listBox = new ListBox(...);
            textBox = new TextBox(...);
            addButton = new Button(...);
            addButton.Click += delegate {
                  listBox.Items.Add(textBox.Text);
            };
      }
}
An anonymous method consists of the keyword delegate, an optional parameter list, and a statement list enclosed in { and } delimiters. The anonymous method in the previous example doesn’t use the parameters supplied by the delegate, and it can therefore omit the parameter list. To gain access to the parameters, the anonymous method can include a parameter list:
addButton.Click += delegate(object sender, EventArgs e) {
      MessageBox.Show(((Button)sender).Text);
};
In the previous examples, an implicit conversion occurs from the anonymous method to the EventHandler delegate type (the type of the Click event). This implict conversion is possible because the parameter list and return type of the delegate type are compatible with the anonymous method. The exact rules for compatibility are as follows:
·         The parameter list of a delegate is compatible with an anonymous method if one of the following is true:
o        The anonymous method has no parameter list and the delegate has no out parameters.
o        The anonymous method includes a parameter list that exactly matches the delegate’s parameters in number, types, and modifiers.
·         The return type of a delegate is compatible with an anonymous method if one of the following is true:
o        The delegate’s return type is void and the anonymous method has no return statements or only return statements with no expression.
o        The delegate’s return type is not void and the expressions associated with all return statements in the anonymous method can be implicitly converted to the return type of the delegate.
Both the parameter list and the return type of a delegate must be compatible with an anonymous method before an implicit conversion to that delegate type can occur.
The following example uses anonymous methods to write functions “in-line.” The anonymous methods are passed as parameters of a Function delegate type.
using System;
delegate double Function(double x);
class Test
{
      static double[] Apply(double[] a, Function f) {
            double[] result = new double[a.Length];
            for (int i = 0; i < a.Length; i++) result[i] = f(a[i]);
            return result;
      }
      static double[] MultiplyAllBy(double[] a, double factor) {
            return Apply(a, delegate(double x) { return x * factor; });
      }
      static void Main() {
            double[] a = {0.0, 0.5, 1.0};
            double[] squares = Apply(a, delegate(double x) { return x * x; });
            double[] doubles = MultiplyAllBy(a, 2.0);
      }
}
The Apply method applies a given Function to the elements of a double[], returning a double[] with the results. In the Main method, the second parameter passed to Apply is an anonymous method that is compatible with the Function delegate type. The anonymous method simply returns the square of its argument, and thus the result of that Apply invocation is a double[] containing the squares of the values in a.
The MultiplyAllBy method returns a double[] created by multiplying each of the values in the argument array a by a given factor. In order to produce its result, MultiplyAllBy invokes the Apply method, passing an anonymous method that multiplies the argument x by factor.
Local variables and parameters whose scope contains an anonymous method are called outer variables of the anonymous method. In the MultiplyAllBy method, a and factor are outer variables of the anonymous method passed to Apply, and because the anonymous method references factor, factor is said to have been captured by the anonymous method. Ordinarily, the lifetime of a local variable is limited to execution of the block or statement with which it is associated. However, the lifetime of a captured outer variable is extended at least until the delegate referring to the anonymous method becomes eligible for garbage collection.
Method Group Conversation
As described in the previous section, an anonymous method can be implicitly converted to a compatible delegate type. C# 2.0 permits this same type of conversion for a method group, allowing explicit delegate instantiations to be omitted in almost all cases. For example, the statements
addButton.Click += new EventHandler(AddClick);
Apply(a, new Function(Math.Sin));
can instead be written
addButton.Click += AddClick;
Apply(a, Math.Sin);
When the shorter form is used, the compiler automatically infers which delegate type to instantiate, but the effects are otherwise the same as the longer form.
Visitors/Readers Comments
(for questions please use The Forum)



"Be the First to Comment!"


Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)