Emailid
Password
         
  
    Forgot password

New user Sign Up
 

Anonymous Methods in C#

       Current Rating:  0%                                                     Total Members Rated:  0
                                                                     Send To Friend

  

Anonymous Methods in C#

 

 

Introduction

 

Anonymous methods are based on a language concept called a lambda function and are similar to those found in Lisp and Python. They are another practical language construct that allows programmers to create code blocks that can be encapsulated in a delegate and executed at a later time.

 

 

Delegate Code and its construction

 

When we come to know about a delegate, it is an object that references a method. Whenever the delegate is invoked, the method that it references is called. In the following example, a simple form is illustrated with three controls-a list box, a text box and a button. When the button is initialized, the program instructs its Click delegate to reference the AddClick method stored elsewhere in the object. In the AddClick method, the value of the text box is stored into the list box. By virtue of being added to the button instance's Click delegate, the AddClick method is called whenever the button is clicked.

public class MyForm { ListBox listBox; TextBox textBox; Button button; public MyForm() { listBox = new ListBox(...); textBox = new TextBox(...); button = new Button(...); button.Click += new EventHandler(AddClick); } void AddClick(object sender, EventArgs e) { listBox.Items.Add(textBox.Text); } }

 

 

On applying Anonymous Methods

 

A separate function is created, it is referenced by the delegate, and whenever the delegate is invoked, the program calls the function. Inside the function, a series of executable steps are performed. With the addition of anonymous methods, your program could forego creating an entirely new method for the class and instead directly reference the executable steps contained therein from the delegate. Anonymous methods are declared by instantiating a delegate, then following the instantiation statement with a curly brace pair that denotes a scope of execution, and a semicolon to conclude the report.

Here, your program modifies the delegate creation statement to directly modify the list box rather than referencing a function that modifies the list box on behalf of your program. The code is stored to modify the list box within the scope of execution immediately following the delegate creation of statement.

 

public class MyForm { ListBox listBox; TextBox textBox; Button button; public MyForm() { listBox = new ListBox(...); textBox = new TextBox(...); button = new Button(...); button.Click += new EventHandler(sender, e) { listBox.Items.Add(textBox.Text); }; } }

 

 

Anonymous methods can refer variables declared by the class as well as by parameters or local variables declared in the method in which it resides. Notice how code within the Anonymous method can access and manipulate variables declared outside its span.

 

 

Anonymous Methods and Passing Parameters 

 

The Anonymous method statement includes two parameters, named sender and e. Looking at the definition of the Button class' Click delegate, you find that any function referenced by the delegate must include two parameters; the first of type object, the second of type EventArgs. In the first example, without using Anonymous methods, your program passed two parameters to the AddClick method, an object and an EventArgs.

The names of the two parameters must be declared so that the associated code block can use them in the Anonymous method. The delegate must still receive two parameters even though the code is written inline. When the Click event on the button is triggered, the Anonymous method is invoked and the appropriate parameters are passed to it.

 

 

Anonymous Methods and its procedure

 

The C# compiler automatically converts the code in its scope of execution into a uniquely named function within a uniquely named class when an Anonymous delegate is encountered. The delegate in which the code block is stored is then set to reference the compiler-generated object and method. When the delegate is invoked, the Anonymous method block is executed via the compiler-created method.

 

All the best!


                           Rate This Article:   

Author is Offline
  Author: Konjanja Takeuchi
       


Comments Posted
Label
Subject Author Status Date

 

Post Comment

Related Articles
N-Tier in ASP.NET or other .NET apps
Software Development Outsourcing (Offshore ) to India
Make your web site ‘perfect and Search Engine Friendly for Google and Yahoo
Color combination of your Web site
An appealing design for your Web site



Home | About Us | Site Map | Privacy Policy | Submit Links