Last Updated : 11 Jan, 2025
A delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods. It provides a way which tells which method is to be called when an event is triggered.
For example, if you click on a Button on a form (Windows Form application), the program would call a specific method. In simple words, it is a type that represents references to methods with a particular parameter list and return type and then calls the method in a program for execution when it is needed.
Declaration of DelegatesDelegate type can be declared using the delegate keyword. Once a delegate is declared, delegate instance will refer and call those methods whose return type and parameter-list matches with the delegate declaration.
Syntax:
[modifier] delegate [return_type] [delegate_name] ([parameter_list]);
Example:
// "public" is the modifier
// "int" is return type
// "GeeksForGeeks" is delegate name
// "(int G, int F, int G)" are the parameters
public delegate int GeeksForGeeks(int G, int F, int G);
Note: A delegate will call only a method which agrees with its signature and return type. A method can be a static method associated with a class or can be an instance method associated with an object, it doesn't matter.
Instantiation & Invocation of DelegatesAfter declaring a delegate, a delegate object is created with the help of new keyword. Once a delegate is instantiated, a method call made to the delegate is pass by the delegate to that method. The parameters passed to the delegate by the caller are passed to the method, and the return value, if any, from the method, is returned to the caller by the delegate. This is known as invoking the delegate.
Syntax:
[delegate_name] [instance_name] = new [delegate_name](calling_method_name);
Example:
CSharp
// Using of Delegates
using System;
class Geeks
{
// Declaring the delegates
// Here return type and parameter type should
// be same as the return type and parameter type
// of the two methods
// "addnum" and "subnum" are two delegate names
public delegate void addnum(int a, int b);
public delegate void subnum(int a, int b);
public void sum(int a, int b)
{
Console.WriteLine("(100 + 40) = {0}", a + b);
}
public void subtract(int a, int b)
{
Console.WriteLine("(100 - 60) = {0}", a - b);
}
// Main Method
public static void Main(String []args)
{
// creating object "obj" of class "Geeks"
Geeks obj = new Geeks();
// creating object of delegate, name as "del_obj1"
// for method "sum" and "del_obj2" for method "subtract" &
// pass the parameter as the two methods by class object "obj"
// instantiating the delegates
addnum del_obj1 = new addnum(obj.sum);
subnum del_obj2 = new subnum(obj.subtract);
// pass the values to the methods by delegate object
del_obj1(100, 40);
del_obj2(100, 60);
// These can be written as using
// "Invoke" method
// del_obj1.Invoke(100, 40);
// del_obj2.Invoke(100, 60);
}
}
Output:
(100 + 40) = 140
(100 - 60) = 40
Explanation: In the above program, there are two delegates addnum and subnum. We are creating the object obj of the class Geeks because both the methods(addnum and subnum) are instance methods. So they need an object to call. If methods are static then there is no need to create the object of the class.
Multicasting of a DelegateMulticasting of delegate is an extension of the normal delegate(sometimes termed as Single Cast Delegate). It helps the user to point more than one method in a single call.
Properties:
Note: Remember, multicasting of delegate should have a return type of Void otherwise it will throw a runtime exception. Also, the multicasting of delegate will return the value only from the last method added in the multicast. Although, the other methods will be executed successfully.
Below program demonstrates the use of Multicasting of a delegate:
CSharp
// C# program to illustrate the
// Multicasting of Delegates
using System;
class rectangle {
// declaring delegate
public delegate void rectDelegate(double height,
double width);
// "area" method
public void area(double height, double width)
{
Console.WriteLine("Area is: {0}", (width * height));
}
// "perimeter" method
public void perimeter(double height, double width)
{
Console.WriteLine("Perimeter is: {0} ", 2 * (width + height));
}
// Main Method
public static void Main(String []args)
{
// creating object of class
// "rectangle", named as "rect"
rectangle rect = new rectangle();
// these two lines are normal calling
// of that two methods
// rect.area(6.3, 4.2);
// rect.perimeter(6.3, 4.2);
// creating delegate object, name as "rectdele"
// and pass the method as parameter by
// class object "rect"
rectDelegate rectdele = new rectDelegate(rect.area);
// also can be written as
// rectDelegate rectdele = rect.area;
// call 2nd method "perimeter"
// Multicasting
rectdele += rect.perimeter;
// pass the values in two method
// by using "Invoke" method
rectdele.Invoke(6.3, 4.2);
Console.WriteLine();
// call the methods with
// different values
rectdele.Invoke(16.3, 10.3);
}
}
Output:
Area is: 26.46Important Points About Delegates:
Perimeter is: 21Area is: 167.89
Perimeter is: 53.2
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4