All About Delegate In C#

I will explain what is delegate, why we use it and how many types of delegates with example.

What is Delegate

It is a type safe function pointer that holds the reference to function [Method]. Delegate is a type which represents the reference of a Method. The signature of the delegate must be same as method that is why we call the delegate as a type safe function pointer.

It is very similar to a class because we can also create instance of the delegate. When you create instance of delegate, you need to pass the method name as a parameter to delegate instance, so here delegate points to that method.

What is Delegate

The following is the general syntax for delegate.

public delegate int MyDelegate(int x, int y);

Why Delegate

  1. You can call or invoke method through the Delegate instance.
  2. It is used to pass the Method as arguments to other method.
  3. It is used to define the callback methods.
  4. Events are used with delegate.

Delegate
Example

  1. using System;  
  2. namespace ConsoleDemo  
  3. {  
  4.     public delegate void SampleDelegate(string msg);  
  5.     public class Program  
  6.     {  
  7.   
  8.         public static void Show(string message)  
  9.         {  
  10.             Console.WriteLine(message);  
  11.         }  
  12.         public static void Main(string[] args)  
  13.         {  
  14.             SampleDelegate objSampleDelegate = new SampleDelegate(Show);  
  15.             objSampleDelegate("Welcome to sample delegate");  
  16.             Console.ReadLine();  
  17.         }  
  18.     }  
  19. }  
Output

result

Multicast Delegate

It is a type of delegate which references more than one method. Call or Invoke the Multicast delegate invoking all methods which is referenced to it. It invokes the list of method in the same order in which they are added.

Multicast Delegate image

The following are the ways to attach or detach the method to the delegate.

 

  • + or += to register a function or method with the delegate.
  • – or -= to register a function or method with the delegate.

Note: A multicast delegate, invoke the methods in the invocation list, in the same order in which they are added.

If delegate return type is not void and it is multicast delegate then the value of last invoked method will return. Observer Design pattern is a simple example where we will see the use of multicast delegate. It is also called Publisher/Subscriber Pattern.

Example 1

  1. using System;  
  2. namespace ConsoleDemo  
  3. {  
  4.     public delegate void SampleMulticastDelegate();  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             SampleMulticastDelegate objDelegate1, objDelegate2, objDelegate3, objDelegate4;  
  10.             objDelegate1 = new SampleMulticastDelegate(Show1);  
  11.             objDelegate2 = new SampleMulticastDelegate(Show2);  
  12.             objDelegate3 = new SampleMulticastDelegate(Show3);  
  13.             objDelegate4 = objDelegate1 + objDelegate2 + objDelegate3;  
  14.             objDelegate4(); //Here del4 holding reference of 3 Show.  
  15.             Console.Read();  
  16.         }  
  17.         public static void Show1()  
  18.         {  
  19.             Console.WriteLine("Show one is invoked");  
  20.         }  
  21.         public static void Show2()  
  22.         {  
  23.             Console.WriteLine("Show two is invoked");  
  24.         }  
  25.         public static void Show3()  
  26.         {  
  27.             Console.WriteLine("Show three is invoked");  
  28.         }  
  29.     }  
  30. }  
Output

run

Example 2
  1. using System;  
  2. namespace ConsoleDemo  
  3. {  
  4.     public delegate void SampleMulticastDelegate();  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             SampleMulticastDelegate objDelegate = new SampleMulticastDelegate(Show1);  
  10.             objDelegate += Show2;  
  11.             objDelegate += Show3;  
  12.             objDelegate -= Show2; // Remove Show2 Method using - sign  
  13.             objDelegate();  
  14.             Console.Read();  
  15.             Console.Read();  
  16.         }  
  17.         public static void Show1()  
  18.         {  
  19.             Console.WriteLine("Show one is invoked");  
  20.         }  
  21.         public static void Show2()  
  22.         {  
  23.             Console.WriteLine("Show two is invoked");  
  24.         }  
  25.         public static void Show3()  
  26.         {  
  27.             Console.WriteLine("Show three is invoked");  
  28.         }  
  29.     }  
  30. }  
Output

Output

multicast delegate

Thanks for reading the article. Hope you all enjoyed it.

 

Up Next
    Ebook Download
    View all
    Learn
    View all