Delegate in C#
Delegate in C#
Previous Next
C# - Delegate
A function can have one or more parameters of different data types, but what if you
want to pass a function itself as a parameter? How does C# handle the callback
functions or event handler? The answer is - delegate.
A delegate is like a pointer to a function. It is a reference type data type and it holds
the reference of a method. All the delegates are implicitly derived from
System.Delegate class.
Delegate Syntax:
The Print delegate shown above, can be used to point to any method that has same
return type & parameters declared with Print. Consider the following example that
declares and uses Print delegate.
Example: C# delegate
class Program
{
// declare delegate
public delegate void Print(int value);
https://www.tutorialsteacher.com/csharp/csharp-delegates 1/8
1/24/2020 Delegate in C#
// or
// Print printDel = new Print(PrintNumber);
printDel(100000);
printDel(200);
printDel(10000);
printDel(200);
}
Try it
Output:
Number: 10,000
Number: 200
Money: $ 10,000.00
Money: $ 200.00
In the above example, we have declared Print delegate that accepts int type
parameter and returns void. In the Main() method, a variable of Print type is
declared and assigned a PrintNumber method name. Now, invoking Print delegate
https://www.tutorialsteacher.com/csharp/csharp-delegates 2/8
1/24/2020 Delegate in C#
will in-turn invoke PrintNumber method. In the same way, if the Print delegate
variable is assigned to the PrintMoney method, then it will invoke the PrintMoney
method.
delegate in C#
Optionaly, a delegate object can be created using the new operator and specify a
method name, as shown below:
Invoking Delegate
The delegate can be invoked like a method because it is a reference to a method.
Invoking a delegate will in-turn invoke a method which id refered to. The delegate
can be invoked by two ways: using () operator or using the Invoke() method of
delegate as shown below.
https://www.tutorialsteacher.com/csharp/csharp-delegates 3/8
1/24/2020 Delegate in C#
//or
printDel(10000);
Number: 10000
Number: 10000
In the above example, PrintHelper method has a delegate parameter of Print type
and invokes it like a function: delegateFunc(numToPrint) .
The following example shows how to use PrintHelper method that includes delegate
type parameter.
class Program
{
public delegate void Print(int value);
https://www.tutorialsteacher.com/csharp/csharp-delegates 4/8
1/24/2020 Delegate in C#
delegateFunc(numToPrint);
}
Try it
Output:
Number: 10,000
Money: $ 10,000.00
Multicast Delegate
The delegate can points to multiple methods. A delegate that points multiple
methods is called a multicast delegate. The "+" operator adds a function to the
delegate object and the "-" operator removes an existing function from a delegate
object.
printDel(1000);
printDel -=PrintHexadecimal;
printDel(2000);
}
https://www.tutorialsteacher.com/csharp/csharp-delegates 5/8
1/24/2020 Delegate in C#
Try it
Output:
Number: 1,000
Hexadecimal: 3EB
Money: $ 1,000.00
Number: 2,000
Money: $2,000.00
As you can see in the above example, Print delegates becomes a multicast delegate
because it points to three methods - PrintNumber, PrintMoney & PrintHexadecimal.
So invoking printDel will invoke all the methods sequentially.
Delegate is also used with Event, Anonymous method, Func delegate, Action
delegate.
Points to Remember :
3) A method that is going to assign to delegate must have same signature as delegate.
https://www.tutorialsteacher.com/csharp/csharp-delegates 6/8