Anonymous Method in C# (original) (raw)

Last Updated : 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 :

CSharp `

// C# program to illustrate how to // create an anonymous function using 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");
}

}

`

Output:

My favorite pet is: Dog

Important Points:

Similar Reads

Introduction






Fundamentals







Control Statements





OOP Concepts






Methods






Arrays






ArrayList




String






Tuple





Indexers