Anonymous Methods in C#

In c#, anonymous methods are the methods without a name. Generally, the anonymous methods can define by using the delegate keyword and can be assigned to the variable of the delegate type.

 

Following is the sample way of creating anonymous methods in c# using the delegate keyword.

 

// Create a delegate
public delegate void MathOps(int a, int b);
// Instantiate the delegate using an anonymous method
MathOps ops = delegate(int x, int y)
{
Console.WriteLine("Add Result: {0}", x + y);
Console.WriteLine("Subtract Result: {0}", x - y);
};

If you observe the above code, we created an anonymous method, passed it as a delegate parameter, and assigned it to a variable of the delegate type.

 

In c#, anonymous methods are useful to reduce the coding overhead in instantiating delegates because we don’t have to create a separate method.

 

In anonymous methods, if we define any parameters, those can access only within the anonymous method block, but the anonymous methods can access the variables which we define in outer functions.

C# Anonymous Method Example

Following is the example of creating anonymous methods in c# using the delegate keyword.

 

using System;

namespace TutlaneExamples
{
    class Program
    {
       // Create a delegate
       public delegate void MathOps(int a);
       static void Main(string[] args)
       {
          int y = 10;
          // Instantiate the delegate using an anonymous method
          MathOps ops = delegate(int x)
          {
             Console.WriteLine("Add Result: {0}", x + y);
             Console.WriteLine("Subtract Result: {0}", x - y);
          };
          ops(90);
          Console.ReadLine();
       }
    }
}

If you observe the above code, we created an anonymous method, passing it as a delegate parameter, and we are able to access outside variables within the anonymous method.

 

When we execute the above code, we will get the result as shown below.

 

Add Result: 100
Subtract Result: 80

C# Send Anonymous Method as Parameter

In c#, we can pass an anonymous method as a parameter to the method that accepts delegate as a parameter.

 

Following is the example of sending an anonymous method as a parameter to another method in c#.

 

using System;

namespace TutlaneExamples
{
    class Program
    {
       // Create a delegate
       public delegate void MathOps(int a);
       static void Main(string[] args)
       {
          int y = 10;
          // Instantiate the delegate using an anonymous method
          MathOps ops = delegate(int x)
          {
             Console.WriteLine("Add Result: {0}", x + y);
             Console.WriteLine("Subtract Result: {0}", x - y);
          };
          GetInfo(ops, 90);
          Console.ReadLine();
       }
       static void GetInfo(MathOps ops, int k)
       {
         ops(k);
       }
    }
}

If you observe the above code, we created an anonymous method and passing it as a parameter to the other method (GetInfo).

 

When we execute the above code, we will get the result as shown below.

 

Add Result: 100
Subtract Result: 80

This is how we can send an anonymous method as a parameter to another method in c# based on our requirements.

C# Anonymous Methods Drawbacks

Following are some of the limitations of anonymous methods in c#.

 

  • In c#, we cannot use jump statements such as goto, break and continue inside of an anonymous method.
  • Anonymous methods cannot access ref or out parameters of an outer scope.
  • The unsafe code cannot access within the anonymous method.
  • Anonymous methods are not allowed on the left side of is operator.

Actually, the anonymous methods are introduced in C# 2.0 to create methods without a name, but in C# 3.0 and later versions, the lambda expressions have been introduced to achieve better functionality than anonymous methods by writing inline code. To learn more about lambda expressions, check LINQ Lambda Expressions.

C# Anonymous Methods Overview

Following are the important points which we need to remember about the anonymous methods in c#.

 

  • Anonymous methods are methods without a name. 
  • We can define anonymous methods using the delegate keyword and can be assigned to the variable of the delegate type.
  • Anonymous methods are useful to reduce the coding overhead in instantiating delegates because we don’t have to create a separate method.
  • In anonymous methods, if we define any parameters, those can access within the anonymous method block.
  • We can pass anonymous methods as a parameter to the other methods.