Understanding Delegate In C#, With Example

Hi!

Today's C# topic, Delegates,  is very interesting. Some say, it’s very complex. I hope you have worked on delegate and know about it. However, for freshers, it is a very complex topic. So, I am going to explain it in a simple way. I hope, after that, you will get the basic idea of delegate and love to use delegates in your project.

What is Delegate?

If you have got a chance to work with C++, then you definitely know about function pointer in C++.

Delegate is just like the function pointer in C#, but delegates are type safe. Delegate is an object that points to another method either static or instance. It takes arguments and returns value. It takes reference to other methods. We can invoke or call the method through the delegate object.

Points to remember regarding Delegate
  1. Using Delegates, we can pass methods and parameters.
  2. Using Delegates, we can define callback methods.
  3. We can call multiple methods on a single call using Delegate.

To use Delegate, there are only four steps - 

  1. Declare a delegate type.
  2. Create or find a method which has same type of delegate.
  3. Create object/instance of Delegate.
  4. And at last, Invoke Delegate object.

Declare a Delegate type

First step to start with delegate is to declare the delegate type. The “delegate” keyword is used to define a delegate type.

  1. //Declare a Delegate  
  2. delegate int DotnetTutorialDelegate(int x, int y, int z);  
  3. //Declare a Generic Delegate  
  4. public delegate T Add<T>(T x, T y, T z);  
Create or Find a method which has same signature as Delegate

To use delegate, you have methods or to create methods which have same signature of delegate. We can use instance and static methods with delegate.

Create instance of Delegate
  1. //For simple delegate   
  2. DotnetTutorialDelegate objDelegate1 = new DotnetTutorialDelegate(DelegateExample.Average);  
  3.   
  4. //For Generic type delegate  
  5. Add<double> objDoubleTypeDelegate = GenericDelegateExample.AddDouble;  
Invoke Delegate object
  1. //First Way to call delegate  
  2. Console.WriteLine("value is " + objDelegate1.Invoke(3, 8, 9));  
  3.   
  4. //Second Way to call delegate  
  5. Console.WriteLine("value is " + objDelegate1(3, 4, 5));  
Full Example of Simple Delegate
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace NextWithDeleage  
  7. {  
  8.     //Declare a Delegate  
  9.     delegate int DotnetTutorialDelegate(int x, int y, int z);  
  10.     public class DelegateExample  
  11.     {  
  12.         //instance method  
  13.         public int Add(int a, int b, int c)  
  14.         {  
  15.             return (a + b + c);  
  16.         }  
  17.         //static method  
  18.         public static int Average(int a, int b, int c)  
  19.         {  
  20.             return (a + b + c) / 3;  
  21.         }  
  22.     }  
  23.       
  24.     class Program  
  25.     {  
  26.         static void Main(string[] args)  
  27.         {  
  28.             //instance of delegate  
  29.             DotnetTutorialDelegate objDotnetTutorialDelegate;  
  30.   
  31.             DelegateExample clsDelegateExample = new DelegateExample();  
  32.   
  33.             //reference of instance method to delegate instance  
  34.             objDotnetTutorialDelegate = clsDelegateExample.Add;  
  35.   
  36.             //First Way to call delegate  
  37.             Console.WriteLine("value is " + objDotnetTutorialDelegate(3, 3, 2));  
  38.   
  39.             //reference of static method to delegate instance  
  40.             DotnetTutorialDelegate objDelegate1 = new DotnetTutorialDelegate(DelegateExample.Average);  
  41.   
  42.             //Second Way to call delegate  
  43.             Console.WriteLine("value is " + objDelegate1.Invoke(3, 8, 9));  
  44.   
  45.             //Third Way to call delegate  
  46.             Console.WriteLine("value is " + objDelegate1(3, 4, 5));  
  47.             Console.ReadLine();  
  48.               
  49.         }  
  50.     }  
  51. }  
Full Example of Generic Delegate
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace NextWithDeleage  
  7. {  
  8.     //Declare a generic delegate  
  9.     public delegate T Add<T>(T x, T y, T z);  
  10.   
  11.     public static class GenericDelegateExample  
  12.     {  
  13.         public static int AddInt(int x, int y, int z)  
  14.         {  
  15.             return (x + y + z);  
  16.         }  
  17.         public static double AddDouble(double  x, double y, double z)  
  18.         {  
  19.             return (x+y+z);  
  20.         }  
  21.     }  
  22.   
  23.     class Program  
  24.     {  
  25.         static void Main(string[] args)  
  26.         {  
  27.             Add<int> objIntTypeDelegate = new Add<int>(GenericDelegateExample.AddInt);  
  28.   
  29.             //A new way to reference method to delegate instace  
  30.             Add<double> objDoubleTypeDelegate = GenericDelegateExample.AddDouble;  
  31.   
  32.             Console.WriteLine("Sum of 2, 3 and 4 is " + objIntTypeDelegate(2, 3, 4));  
  33.             Console.WriteLine("Sum of 2.3, 3.4 and 4.5 is " + objDoubleTypeDelegate(2.3, 3.4, 4.5));  
  34.   
  35.             Console.ReadLine();  
  36.         }  
  37.     }  
  38. }  
Conclusion

Today, we learned about Delegates in C# and what is the use of it. We also learned how to create simple delegate and how to create a generic delegate. Hope you enjoyed.

Reference

If you want to know more, please click here.

I hope this post helps you. Please put your feedback using comment which will help me improve for the next post. If you have any doubts, please ask your doubts or query in the comment section. 
Next Recommended Reading Delegates With A Real Time Example In C#