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.