How And When To Use Delegates In Your Project

In this article, I will explain about delegates, type of delegates, and when to use delegates.

After reading this article, you will learn the following.

  • What is delegate?
  • Where to use delegates
  • Types of delegates
  • When to use delegates

What is a Delegate?

Delegate is similar to a function pointer in C & C++ but the delegates are user defined types in C#. Make a note that delegates are not a member of class, but similar to a class. These are the backbone for events.

When to use delegates?

I believe that a lot of people can answer the "what is a delegate?" question in interviews but are not able to explain when to use it. No worries! Let me tell you a few important points about delegates.

  1. These are used to represent or refer to one or more functions.
  2. These can only be used to define call-back methods.
  3. In order to consume a delegate, we need to create an object to delegate.

How many types of delegates are present?

There are two types of delegates available.

  1. Single Cast Delegate
  2. Multi Cast Delegate

Single Cast Delegate

A delegate that represents only a single function is known as Single Cast Delegate.

Multi Cast Delegate

A delegate that represents more than one function is known as Multi Cast Delegate.

Let's see a sample program on these two types.

Before that, we shall know the steps required to work with delegates.

  1. Creating a Delegate
  2. Instantiating a Delegate
  3. Invoking a Delegate

Eg

Let us consider a function.

  1. Public void Add(int a, int b) {  
  2.     Code…..  
  3. }   

Referring to the above function, if we want to use the delegate, we use the aforementioned three steps as below.

Step 1 Creating a Delegate

Syntax

Access modifier delegate return type Delegate name ([arguments list]);

Eg

  1. Public delegate void Delegate (int x, int y);   

When we create a delegate, Access modifier, return type, number of arguments, and their data types of the delegate must and should be the same as Access modifier, return type, number of arguments and their data types of the function that we want to refer to.

Step 2 Instantiating the Delegate

Syntax

Delegate name object name = new Delegate name (Targe Function name);

Eg

  1. Delegate obj= new Delegate(Add);   

What exactly we are doing in this step is that a reference will be maintained from the delegate object to the function that we want to refer to.

Step 3 Invoking the Delegate

Syntax

Object Name ([Arguments Values])

Eg

  1. Obj(10,20)   

At this step, the function that is referred by the delegate will be called for the execution. So now, I hope you are clear about what delegate is,when to use delegate, and steps to work with delegates.

Now, let's start with a sample program on Single & Multiple delegates.

Single Cast Delegate

Let’s see a sample program on Single Delegate to refer a function in the same class.

Code 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace DelegatesDemo {  
  7.     class Program {  
  8.         static void Display(string S) {  
  9.             Console.WriteLine("My Name is :" + S);  
  10.         }  
  11.         delegate void X(string a);  
  12.         static void Main(string[] args) {  
  13.             X objD = new X(Display);  
  14.             objD("Rathrola Prem Kumar");  
  15.             Console.Read();  
  16.         }  
  17.     }  
  18. }   

Output


Let's see another program for single cast delegate to refer a function in a different class.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace DelegatesDemo {  
  7.     class Program {  
  8.         public void Display(string S) {  
  9.             Console.WriteLine("My Designation is :" + S);  
  10.         }  
  11.         public delegate void Delegate(string a);  
  12.         class DelegateDemo {  
  13.             static void Main() {  
  14.                 Program obj1 = new Program();  
  15.                 Delegate objD = new Delegate(obj1.Display);  
  16.                 objD("Technical Specialist");  
  17.                 Console.Read();  
  18.             }  
  19.         }  
  20.     }  
  21. }   

Output


Sample Program for Multi cast delegate 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace DelegatesDemo {  
  7.     class Program {  
  8.         public void Add(int x, int y) {  
  9.             Console.WriteLine("Sum is:" + (x + y));  
  10.         }  
  11.         public void Subtract(int x, int y) {  
  12.             Console.WriteLine("Difference is:" + (x - y));  
  13.         }  
  14.         public void Multiply(int x, int y) {  
  15.             Console.WriteLine("Product is:" + (x * y));  
  16.         }  
  17.         public void Divide(int x, int y) {  
  18.             Console.WriteLine("Quotient is:" + (x / y));  
  19.         }  
  20.     }  
  21.     public delegate void MultiCastDelegate(int a, int b);  
  22.     class ClsDelegate {  
  23.         static void Main() {  
  24.             Program obj1 = new Program();  
  25.             MultiCastDelegate objD = new MultiCastDelegate(obj1.Multiply);  
  26.             objD += obj1.Add;  
  27.             objD += obj1.Substract;  
  28.             objD += obj1.Divide;  
  29.             objD(40, 10);  
  30.             objD -= obj1.Add;  
  31.             objD -= obj1.Divide;  
  32.             objD(50, 10);  
  33.             Console.ReadLine();  
  34.         }  
  35.     }  
  36. }   

Output


To add reference of more functions to a delegate object, we use += operator.

  1. objD += obj1.substract;  
  2. objD += obj1.Add;   

To delete the reference of any function from Delegate object, we use -= operator.

  1. objD -= obj1.substract;  
  2. objD = -obj1.Add;   

I hope, this article was helpful to you.

Up Next
    Ebook Download
    View all

    FileInfo in C#

    Read by 9.7k people
    Download Now!
    Learn
    View all