Introduction
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.
- These are used to represent or refer to one or more functions.
- These can only be used to define call-back methods.
- 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.
- Single Cast Delegate
- 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.
- Creating a Delegate
- Instantiating a Delegate
- Invoking a Delegate
Example
Let us consider a function.
Public void Add(int a, int b) {
Code…..
}
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]);
Example
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);
Example
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]);
Example
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DelegatesDemo {
class Program {
static void Display(string S) {
Console.WriteLine("My Name is: " + S);
}
delegate void X(string a);
static void Main(string[] args) {
X objD = new X(Display);
objD("Rathrola Prem Kumar");
Console.Read();
}
}
}
Output

Let's see another program for single cast delegate to refer a function in a different class.
using System;
namespace DelegatesDemo {
class Program {
public void Display(string S) {
Console.WriteLine("My Designation is: " + S);
}
public delegate void Delegate(string a);
class DelegateDemo {
static void Main() {
Program obj1 = new Program();
Delegate objD = new Delegate(obj1.Display);
objD("Technical Specialist");
Console.Read();
}
}
}
}
Output

Sample Program for Multi cast delegate
using System;
namespace DelegatesDemo {
class Program {
public void Add(int x, int y) {
Console.WriteLine("Sum is: " + (x + y));
}
public void Subtract(int x, int y) {
Console.WriteLine("Difference is: " + (x - y));
}
public void Multiply(int x, int y) {
Console.WriteLine("Product is: " + (x * y));
}
public void Divide(int x, int y) {
Console.WriteLine("Quotient is: " + (x / y));
}
}
public delegate void MultiCastDelegate(int a, int b);
class ClsDelegate {
static void Main() {
Program obj1 = new Program();
MultiCastDelegate objD = new MultiCastDelegate(obj1.Multiply);
objD += obj1.Add;
objD += obj1.Subtract;
objD += obj1.Divide;
objD(40, 10);
objD -= obj1.Add;
objD -= obj1.Divide;
objD(50, 10);
Console.ReadLine();
}
}
}
Output

To add reference of more functions to a delegate object, we use += operator.
objD += obj1.substract;
objD += obj1.Add;
To delete the reference of any function from Delegate object, we use -= operator.
objD -= obj1.substract;
objD = -obj1.Add;
I hope, this article was helpful to you.

Shiv Ratan KumarPosted Dec 5, 2019, 6:21 AM
Direct Call means your code is a tight couple. It indicates that your client code knows which method you are going to call. But in the case of Delegate call, your client will know only about delegate. So, the delegate will create loosely coupling. Same as that, when your User Interface will directly call your Business logic. Whenever you are going to change in your Business logic then you have to change in client code as well. But by using delegates your UI code will to an abstract pointer and that will access that business logic code. Hence the UI needs not to about the business logic.
Ankur GuptaPosted Aug 7, 2019, 7:42 AM
There is no general scenario here and failed to explain the importance of delegate if we have other option too, Also is there any scenario that can only possible with delegates ?
Hassan AliPosted Jun 10, 2019, 4:19 AM
Nice to see you! Please include some practical type scenario and try to explain the importance of delegates in that case.
Ramzanali MominPosted Oct 29, 2018, 12:53 AM
Nice article
Bikesh SrivastavaPosted Jan 25, 2017, 8:43 AM
Very nice article ,well explained.
Humayun Kabir MamunPosted Jan 24, 2017, 10:40 AM
Thanks for this nice article...
Mahesh AllePosted Jan 24, 2017, 4:30 AM
The section, "When to use delegates?" explaining the "What are delegates?". The point number 2 is correct and other 1 & 3 are more about describing the what are delegates. So delegates are useful to offer to the user of your objects some ability to customize their behavior. Most of the time, you can use other ways to achieve the same purpose and I do not believe you can ever be forced to create delegates. It is just the easiest way in some situations to get the thing done. This link http://stackoverflow.com/questions/1999282/do-this-without-using-an-if-ifs-value1-else-ifs-value2/1999364#1999364 will actually give the example how to convert your if statement to delegates. Hope you understood the when to use the delegates.
Shankar VermaPosted Jan 24, 2017, 3:02 AM
In above example, I'm looking for why we use delegate? But there is nothing in this article what the difference if I'm using directly function what's the pron and cons. I want real time example. Can you please provide a suitable example which is clarify to use delegate