Delegates are amongst the most powerful constructs in the C# language (Full Solution Download at the end of this article).

Some uses of delegates in c# include :

  1. Delaying invocation(calling) of a method
  2. Dynamically assigning target(s) to a method
  3. Advanced eventing patterns using c# delegates – including publish subscribe

Pre-C# 3.0 days

In pre c# 3.0 days, the most common way of declaring and using a delegate in c# was:

Code Snippet
  1. public class PreLambdaDelegate
  2. {
  3.     // declare the delegate
  4.     delegate int CalculateSum(int inta, int intb);
  5.  
  6.     // Instantiate delegate and call (Invoke) the method that the delegate points to
  7.     public int InstantiateAndCallDelegate()
  8.     {
  9.         CalculateSum calculateDelegateInstance = new CalculateSum(DoActualCalculation);
  10.         return calculateDelegateInstance.Invoke(3, 5);       
  11.     }
  12.  
  13.     private int DoActualCalculation(int int1, int int2)
  14.     {
  15.         return int1 + int2;
  16.     }
  17. }

The code is self-explanatory – we declare the delegate, then instantiate it, then invoke it (invoke the method that it is pointing to).

Post C# 3.0 days – Enter Lambda Expressions

Lambda expressions provide a more compact way of accomplishing the same result as above:

Code Snippet
  1. public class PostLambdaExpressionsDelegate
  2. {
  3.     // declare delegate (this part is the same as PreLambdaWay)
  4.     delegate int CalculateSum(int inta, int intb);
  5.  
  6.     // create instance and call delegate using an anonymous method
  7.     public int InstantiateAndCall()
  8.     {
  9.         CalculateSum doCalc = (x, y) =>
  10.         {
  11.             return x + y;
  12.         };
  13.         return doCalc(5,6);
  14.     }
  15. }

 

Notice that we did not need to actually instantiate the delegate. All we had to do was call the method by using a lambda expression (x,y) and an assignment operator (=>) . With the help of these two constructs (lambda expression and an assignment operator), our code is simplified.  To run the samples above, use a simple main program as shown below:

Code Snippet
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         PreLambdaDelegate preLambda = new PreLambdaDelegate();
  6.         int result1 = preLambda.InstantiateAndCallDelegate();
  7.         Console.WriteLine(“Pre Lambda Delegate Result  = “ + result1);
  8.  
  9.         PostLambdaExpressionsDelegate postLambda = new PostLambdaExpressionsDelegate();
  10.         int result2 = postLambda.InstantiateAndCall();
  11.         Console.WriteLine(“Post LambdaExpressions Delegate Result = “ + result2);
  12.     }
  13. }

Download Full Solution – delegates in c# (Zipped)

Anuj holds professional certifications in Google Cloud, AWS as well as certifications in Docker and App Performance Tools such as New Relic. He specializes in Cloud Security, Data Encryption and Container Technologies.

Initial Consultation

Anuj Varma – who has written posts on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.