C# - Anonymous Method

 An anonymous method is a method without a name. 

-- 

Anonymous methods in C# can be defined using the delegate keyword and can be assigned to a variable of delegate type.

public delegate void Print(int value);

static void Main(string[] args)

{

    Print print = delegate(int val) 

    {         

      Console.WriteLine("Inside Anonymous method. Value: {0}", val); 

    };

    print(100);

}

-- 

Anonymous methods can access variables defined in an outer function.

Example: Anonymous Method

public delegate void Print(int value);

static void Main(string[] args)

{

    int i = 10;

    Print prnt = delegate(int val) 

    {

        val += i;

        Console.WriteLine("Anonymous method: {0}", val); 

    };

    prnt(100);

}

--

Anonymous methods can also be passed to a method that accepts the delegate as a parameter.

In the following example, PrintHelperMethod() takes the first parameters of the Print delegate:

Example: Anonymous Method as Parameter

public delegate void Print(int value);

class Program

{

    public static void PrintHelperMethod(Print printDel,int val)

    { 

        val += 10;

        printDel(val);

    }

    static void Main(string[] args)

    {

      PrintHelperMethod(delegate(int val) { Console.WriteLine("Anonymous method: {0}", val); }, 100);

    }

}

--

Anonymous methods can be used as event handlers:

Example: Anonymous Method as Event Handler

saveButton.Click += delegate(Object o, EventArgs e)

    System.Windows.Forms.MessageBox.Show("Save Successfully!"); 

};