Introduction
A delegate is a reference-type variable whose objects can refer to multiple methods, which can then be invoked later. Instead of calling methods directly, delegates can be used to call these methods.
What is Delegate?
A delegate is a type of safe and object-oriented object that can point to another method or multiple methods, which can then be invoked later. It is a reference type variable that refers to methods. It is the C# equivalent of a function pointer in c/c++. A delegate maintains three important pieces of information, which are as follows.
- Name of the method on which it has to make calls.
- Any argument of the method.
- The return value if any.
Syntax
[modifier] delegate [return_type] [delegate_name]([parameter_list]);
In the above-mentioned syntax, each keyword has a separate meaning which is as follows.
- The modifier defines the access of the delegate.
- Delegate is a keyword that is used to define a delegate.
- return_type specifies the type of value to be returned by the method to which the delegate will make a call.
- delegate_name specifies the name of the delegate as defined by the user.
- parameter_list specifies the list of parameters as required by those methods to which the delegate will make a call.
Upon delegate declaration, the instance of the delegate can point to those methods which are having the same return type and same number of parameters as that of a defined delegate.
For example, let's declare a delegate of integer type having public access specifier and which contains two parameters of integer type.
public delegate int Delegate1(int x, int y);
This particular delegate can point to any method that takes in two integers as parameters and returns an integer. So this delegate can only point to methods having similar signatures as that of a delegate.
Let's assume we have a class that contains methods that a particular delegate will point to, as in the code snippet below.
using System;
using System.Collections.Generic;
using System.Text;
namespace Delegate
{
public delegate int SampleDelegate(int x, int y);
public class NumberClass
{
public static int Add(int x, int y)
{
return x + y;
}
public static int Multiply(int x, int y)
{
return x * y;
}
}
class Program
{
static void Main(string[] args)
{
// Instantiate the delegate named SampleDelegate
SampleDelegate del1 = new SampleDelegate(NumberClass.Add);
// Now it will point to NumberClass.Add
int addResult = del1(5, 5);
Console.WriteLine("5 + 5 = " + addResult);
SampleDelegate del2 = new SampleDelegate(NumberClass.Multiply);
// Now it will point to NumberClass.Multiply
int multiResult = del2(5, 5);
Console.WriteLine("5 * 5 = " + multiResult);
}
}
}
Output
In the above code, we are defining a delegate named SampleDelegate which is of integer type, has a public access specifier, and contains two parameters of integer type.
After declaring the delegate, we are creating a class named NumberClass which contains two methods, Add() and Multiply(). These methods are returning an integer-type value.
Inside the Main() function, we have created an instance of delegate called del1 that points to functions inside the class named MyClass.
So inside the main function, we can create two instances of the delegate named SampleDelegate as follows,
// Instantiate Delegate1 with MyClass.Add method
Delegate1 del1 = new Delegate1(MyClass.Add);
// Instantiate Delegate1 with MyClass.Multiply method
Delegate1 del2 = new Delegate1(MyClass.Multiply);
If we want to invoke this add and multiply method, that is call these methods, we can make use of delegate which will point to these methods and call them as follows.
int result = del1(5, 5);
int multiResult = del2(5, 5);
So instead of calling these methods directly, delegates can be used to call these methods.
Every delegate needs to point or subscribe to some method in some class, and it has to follow the same signature rules. If signature rules are not followed, then the delegate will not be able to point to that method.
Multicasting in Delegates
Delegates have the ability to multicast. A delegate object can maintain a list of methods to call rather than just one single method. So we can subscribe as many methods to delegate as we want.
We can have one delegate pointing to multiple functions having the same signature as that of the delegate. Delegates have the ability to multicast, which means a delegate object can maintain a list of methods to call rather than just one single method.
If we want to add a method to the invocation list of delegate objects, then we can use the += operator for subscribing to more than one method. If we want to remove a method from an invocation list of delegate objects, then we can use the -= operator to unsubscribe a method.
using System;
namespace MultiCasting
{
public delegate void MulticastDelegate(int x, int y);
public class Multi
{
public static void Add(int x, int y)
{
Console.WriteLine("You are in the add method.");
Console.WriteLine(x + "+" + y + "=" + (x + y));
}
public static void Multiply(int x, int y)
{
Console.WriteLine("You are in the multiplication method.");
Console.WriteLine(x + "*" + y + "=" + (x * y));
}
}
class Program
{
static void Main(string[] args)
{
MulticastDelegate del1 = new MulticastDelegate(Multi.Add);
del1 += new MulticastDelegate(Multi.Multiply);
Console.WriteLine("Calling add() and multiply() methods.\n");
del1(6, 5);
del1 -= new MulticastDelegate(Multi.Add);
Console.WriteLine("add() method unsubscribed.\n");
del1(7, 5);
}
}
}
Output
In the above code, the delegate named MultiCast delegate is subscribing to the Multiply method using the += operator. Later in the code, we unsubscribed the add() method here, and then we called the delegate object del1 once again and passed in the two integer values. Now it will just subscribe to the Multiply() method and then call that method.
Summary
In this article, we have demonstrated how to define a delegate, and then with the help of a proper code snippet, we have demonstrated how to use delegate objects to call different methods. We have also shown how to use a single delegate to subscribe to more than one method using the concept of Multicasting.