Anonymous Method in C# - GeeksforGeeks

11 Feb, 2019
An anonymous method is a method which doesn’t contain any name which is introduced in C# 2.0. It is useful when the user wants to create an inline method and also wants to pass parameter in the anonymous method like other methods. An Anonymous method is defined using the delegate keyword and the user can assign this method to a variable of the delegate type.
Syntax:
delegate(parameter_list){
    // Code..
};

Example :
// C# program to illustrate how to // create an anonymous functionusing System;  class GFG {      public delegate void petanim(string pet);      // Main method    static public void Main()    {          // An anonymous method with one parameter        petanim p = delegate(string mypet)        {            Console.WriteLine("My favorite pet is: {0}",                                                 mypet);        };        p("Dog");    }}
My favorite pet is: Dog

Important Points:
This method is also known as inline delegate.
Using this method you can create a delegate object without writing separate methods.
This method can access variable present in the outer method. Such type of variables is known as Outer variables. As shown in the below example fav is the outer variable.Example:// C# program to illustrate how an // anonymous function access variable// defined in outer methodusing System;  class GFG {      // Create a delegate    public delegate void petanim(string pet);      // Main method    static public void Main()    {          string fav = "Rabbit";          // Anonymous method with one parameter        petanim p = delegate(string mypet)        {            Console.WriteLine("My favorite pet is {0}.",                                                 mypet);              // Accessing variable defined            // outside the anonymous function            Console.WriteLine("And I like {0} also.", fav);        };        p("Dog");    }}Output:My favorite pet is Dog.
And I like Rabbit also.

Example:
// C# program to illustrate how an // anonymous function access variable// defined in outer methodusing System;  class GFG {      // Create a delegate    public delegate void petanim(string pet);      // Main method    static public void Main()    {          string fav = "Rabbit";          // Anonymous method with one parameter        petanim p = delegate(string mypet)        {            Console.WriteLine("My favorite pet is {0}.",                                                 mypet);              // Accessing variable defined            // outside the anonymous function            Console.WriteLine("And I like {0} also.", fav);        };        p("Dog");    }}
My favorite pet is Dog.
And I like Rabbit also.

You can pass this method to another method which accepts delegate as a parameter. As shown in the below example:Example :// C# program to illustrate how an // anonymous method passed as a parameterusing System;  public delegate void Show(string x);  class GFG {      // identity method with two parameters    public static void identity(Show mypet,                               string color)    {        color = " Black" + color;        mypet(color);    }      // Main method    static public void Main()    {          // Here anonymous method pass as         // a parameter in identity method        identity(delegate(string color) {            Console.WriteLine("The color"+           " of my dog is {0}", color); },                                 "White");    }}Output:The color of my dog is  BlackWhite

Example :
// C# program to illustrate how an // anonymous method passed as a parameterusing System;  public delegate void Show(string x);  class GFG {      // identity method with two parameters    public static void identity(Show mypet,                               string color)    {        color = " Black" + color;        mypet(color);    }      // Main method    static public void Main()    {          // Here anonymous method pass as         // a parameter in identity method        identity(delegate(string color) {            Console.WriteLine("The color"+           " of my dog is {0}", color); },                                 "White");    }}
The color of my dog is  BlackWhite

In anonymous methods, you are allowed to remove parameter-list, which means you can convert an anonymous method into a delegate.
The anonymous-method-block means the scope of the parameters in the anonymous method.
An anonymous method does not contain jump statements like goto, break, or continue.
An anonymous method does not access unsafe code.
An anonymous method does not access in, ref, and out parameter of the outer scope.
You can not use an anonymous method to the left side of the is operator.
You can also use an anonymous method as an event handler.Example:// C# program to illustrate how an // anonymous method use as a // event handlerMyButton.Click += delegate(Object obj, EventArgs ev){    System.Windows.Forms.MessageBox.Show("Complete without error...!!");}
Example:
// C# program to illustrate how an // anonymous method use as a // event handlerMyButton.Click += delegate(Object obj, EventArgs ev){    System.Windows.Forms.MessageBox.Show("Complete without error...!!");}
CSharp-OOP
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C# | IsNullOrEmpty() Method
Main Method in C#
C# | Dictionary.Add() Method
Lambda Expressions in C#
Different ways to sort an array in descending order in C#
C# | Arrays of Strings
C# | How to use strings in switch statement
C# | List Class
C# | How to check whether a List contains a specified element
How to find the length of an Array in C#