An anonymous method is an inline method and it does not have a name, i.e, it has body only. We can define it using a delegate.

Let's try to understand the Anonymous method with the below example. Let's first create a simple console application.

Go to  File->New Project-> Console Application.

Now, let’s create a simple addition method which returns an integer as shown below.

  1. static int addition(int num1, int num2)  
  2.         {  
  3.             return num1 + num2;  
  4.         }  

Now, let's declare a delegate which will point to the addition function as shown below.

  1. delegate int delAddtion(int num1, int num2);  

Let's assign a method to this delegate and invoke the same by passing the parameters as shown in the below example.

  1. delAddtion objDel= addition;  
  2. Console.WriteLine( objDel.Invoke(4, 4).ToString());  

Now, build and run the application. You will see the below result.

Anonymous Methods In C# 

Below is the complete source code for the same.

  1. class Program  
  2.   {  
  3.       delegate int delAddtion(int num1, int num2);  
  4.       static void Main(string[] args)  
  5.       {  
  6.           delAddtion objDel= addition;  
  7.          Console.WriteLine( objDel.Invoke(4, 4).ToString());  
  8.   
  9.       }  
  10.       static int addition(int num1, int num2)  
  11.       {  
  12.           return num1 + num2;  
  13.       }  
  14.   }  

Now, if you notice that for the single line of or for specific block of code, add we have to write an additional method, which is sometimes an overhead.

So to overcome this, we can use Anonymous method by writing inline code as shown below

  1. delAddtion objDel = delegate (int num1, int num2)  
  2.             {  
  3.                 return num1 + num2;  
  4.             };  
  5.  Console.WriteLine(objDel.Invoke(4, 4).ToString());  

Now build and run the application, you will see same below result

Below is the complete source code for the same,

  1. class Program  
  2.   {  
  3.       delegate int delAddtion(int num1, int num2);  
  4.       static void Main(string[] args)  
  5.       {  
  6.           delAddtion objDel= delegate (int num1, int num2)  
  7.           {  
  8.               return num1 + num2;  
  9.           };  
  10.   
  11.          Console.WriteLine( objDel.Invoke(4, 4).ToString());  
  12.   
  13.       }  
  14.       static int addition(int num1, int num2)  
  15.       {  
  16.           return num1 + num2;  
  17.       }  
  18.   }  

Now, as per above, both example Anonymous method is inline method which can be pointed to delegate. So if you want to avoid creating method which is using delegate then use Anonymous method.

You can also use the Anonymous method for better performance.

Let’s understand the same by the below example.

Now, let's add a big loop for executing the addition method and use stopwatch for measuring time, as shown in the below example.

  1. Stopwatch time = new Stopwatch();  
  2.        time.Reset();  
  3.        time.Start();  
  4.        for (int i = 0; i < 500; i++)  
  5.        {  
  6.            delAddtion objDel = addition;  
  7.           objDel.Invoke(4, 4);  
  8.        }  
  9.    time.Stop();  
  10. Console.Write(time.ElapsedTicks.ToString());  

Now, in the above example, we are executing addition() for 500 times and recording its time using a stopwatch class.

Build and run the application.

Anonymous Methods In C# 

Let’s perform the same activity with the Anonymous method.

  1. Stopwatch time = new Stopwatch();  
  2.             time.Reset();  
  3.             time.Start();  
  4.             for (int i = 0; i < 500; i++)  
  5.             {  
  6.                 delAddtion objDel = delegate (int num1, int num2)  
  7.             {  
  8.                 return num1 + num2;  
  9.             };  
  10.                 objDel.Invoke(4, 4);  
  11.   
  12.                // Console.WriteLine(objDel.Invoke(4, 4).ToString());  
  13.             }  
  14.      time.Stop();  
  15. Console.WriteLine(time.ElapsedTicks.ToString());  

Now, build and run the application.

Anonymous Methods In C# 

So here, if you notice, Anonymous methods have taken lesser time than a named method. Thus, if you want better performance, then you should go for the Anonymous method.