What is use of Anonymous methods in C#? Example.

Anonymous methods provide a technique to pass a code block as a delegate parameter.

Anonymous methods are the methods without a name, just the body. You need not specify the return type in an anonymous method.

It is inferred from the return statement inside the method body.

Example 

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);
}
Share:

No comments:

Post a Comment

Tuesday, 3 September 2019

What is use of Anonymous methods in C#? Example.

Anonymous methods provide a technique to pass a code block as a delegate parameter.

Anonymous methods are the methods without a name, just the body. You need not specify the return type in an anonymous method.

It is inferred from the return statement inside the method body.

Example 

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);
}

No comments:

Post a Comment

Popular

Total Pageviews

Archive