Why are use the delegates in C# and how to pass a parameter in delegate, also define the benefits.

Asked 26-Aug-2021
Viewed 527 times

0

Why are use the delegates in C# and how to pass a parameter in delegate, also define the benefits.


1 Answer


0

The C# delegates are like the function pointer present in C, C++, as you must have read pointers in C, C++ Normally the job of pointers is to hold the reference. The C# delegate is like a reference type variable that holds the reference to a method. And this reference can change at runtime. All delegates derive from System. Delegate Class. If you want to use delegate then you have to use the delegate keyword.
Syntax
access-modifier delegate return type delegate-name(parameters) 
Example 
using System;
public class Program {  //declare delegate  public delegate void DelegateFunction(int a, int b);  public void Sum(int a, int b){   Console.WriteLine('Sum : '+ (a+b));  }  public void Sub(int a, int b){   Console.WriteLine('Substraction : '+ (a-b));  }  public void Mulifly(int a, int b){   Console.WriteLine('Mulification : '+ (a*b));  }
 public static void Main()  {   Program pr=new Program();   //adding sum function in delegate   DelegateFunction delfun=pr.Sum;   delfun(45,12);   //adding sub function in delegate   delfun=pr.Sub;   delfun(45,12);   //adding Mulifly function in delegate   delfun=pr.Mulifly;   delfun(45,12);
 } }
Output
Sum : 57

Substraction : 33
Mulification : 540

Delegates are also called type-safe function pointers because the return type of the function is the same as the return type delegate.
We can also pass the delegate as a parameter, A method can also have a parameter of the delegate type.
access-modifier return-type fuction-name(delegate-name delegate-reference, arguments1, arguments2, … ){
 delegate-reference(arguments1, arguments2, …)
}
Example
using System;
public class Program
{
 //declare delegate
 public delegate void DelegateFunction(int a, int b);
 public void Sum(int a, int b){
  Console.WriteLine('Sum : '+ (a+b));
 }
 public void Sub(int a, int b){
  Console.WriteLine('Substraction : '+ (a-b));
 }
 public void Mulifly(int a, int b){
  Console.WriteLine('Mulification : '+ (a*b));
 }
 // delegate as parameter
 public void DelegateAsParameter(DelegateFunction delegatefunction, int a, int b)
 {
  delegatefunction(a,b);
 }
 public static void Main()
 {
  Program pr=new Program();
  pr.DelegateAsParameter(pr.Sum,45,12);
  pr.DelegateAsParameter(pr.Sub,45,12);
  pr.DelegateAsParameter(pr.Mulifly,45,12);
 }
}
Output
Sum : 57

Substraction : 33
Mulification : 540
Multicast Delegates
A delegate can point to more than one method, this means that a delegate can keep the address of more than one method and call them one by one. That is, a delegate that points to multiple methods is called a multicast delegate.
Example
using System;
public class Program
{
 //declare delegate
 public delegate void DelegateFunction(int a, int b);
 public void Sum(int a, int b){
  Console.WriteLine('Sum : '+ (a+b));
 }
 public void Sub(int a, int b){
  Console.WriteLine('Substraction : '+ (a-b));
 }
 public void Mulifly(int a, int b){
  Console.WriteLine('Mulification : '+ (a*b));
 }
 // delegate as parameter
 public void DelegateAsParameter(DelegateFunction delegatefunction, int a, int b)
 {
  delegatefunction(a,b);
 }
 public static void Main()
 {
  Program pr=new Program();
  // Multicast Delegates adding one by function in one delegate
  DelegateFunction dfun=pr.Sum;
  dfun+=pr.Sub;
  dfun+=pr.Mulifly;
  // calling the delegate
  dfun(45,12);
 }
}
Output
Sum : 57

Substraction : 33
Mulification : 540