Register   |  Login
You are hereHome
 Article Details
Anonymous method with and without parameter

One of the features added as part of language enhancements in dotnet 2.0 is anonymous method.

What is anonymous method?

A delegate with no named method associated with it is an anonymous method.

When do we use it?

When we do not want to have a separate method for a delegate

When we need to access the local variables

 

Example:

With Parameter:

int i = 0;

button1.Click += delegate(object s, RoutedEventArgs e)

            {

                i++;

                string buttonText = (s as Button).Content.ToString();

                MessageBox.Show(buttonText + " Clicked " + i.ToString() + " time(s)");

            };

Without Parameter:

int i = 0;

            button1.Click += delegate

            {

                i++;

                MessageBox.Show("I am clicked "+i.ToString() +" time(s)");

            };


Written By: Kumaravel
Date Posted: 2/10/2009
Number of Views: 443


Comments
You must be logged in to submit a comment.

Return