Delegates in C-Sharp in detail with example  

Delegates in C-Sharp are equivalent to function pointers in C or C++. It is a reference type data type and it holds the reference of a method. System.Delegate is the super class for all delegates that we created in C#.

0 402 Technology | C-Sharp
Tue Dec 25 , 2018

A delegate can be declared using delegate keywordfollowed by a function signature as shown below.


Syntax : Delegate declaration


<access modifier> delegate <data type> delegate_name <parameters>


Example: Declare delegate

public delegate void ConceitCode(int number);

The ConceitCode delegate shown above, can be used to point to any method that has same data type & parameters declared with ConceitCode.


Important Points

  • You can use delegates without parameters or with parameter list.
  • You should follow the same syntax as in the method (If you are referring to the method with two int parameters and int return type, the delegate which you are declaring should be in the same format. That's why it is referred to as type safe function pointer.)

Example: C# delegate

using System;
public delegate int Delegate_Add(int a,int b);
public class ConceitCode
{ 
    public static void Main(string[] args)
    {
        //Creating the Delegate Instance
        Delegate_Add delObj = new Delegate_Add(fn_Add);
      	delObj(10,20);																				
    }

    static int  fn_Add(int no1,int no2)
    {
	Console.WriteLine("Addition of 2 numbers using Delegates");
        int add=no1+no2;
	Console.WriteLine(add);
	return add;
    }
}

Output

Addition of 2 numbers using Delegates
30		

Multiple Delegates

More than one delegate in a class is called Multiple Delegates.


Example: Multiple Delegates

using System;
public delegate int Delegate_Sub(int a,int b);
public delegate int Delegate_Mult(int x,int y);
public class ConceitCode
{
    public static void Main(string[] args)
    {
        //Creating the Delegates Instance
        Delegate_Sub delObjSub = new Delegate_Sub(fn_Sub);
	delObjSub(20,10);
        Delegate_Mult delObjMult = new Delegate_Mult(fn_Mult);
      	delObjMult(10,20);																				
    }

    static int  fn_Sub(int a,int b)
    {
	Console.WriteLine("Subtraction of 2 numbers");
        int sub=a-b;
	Console.WriteLine(sub);
	return sub;
    }
    static int  fn_Mult(int x,int y)
    {
	Console.WriteLine("Multiplication of 2 numbers");
        int mult=x*y;
	Console.WriteLine(mult);
	return mult;
    }
}

Output

Subtraction of 2 numbers
10
Multiplication of 2 numbers
200		

Multicast Delegates

A Delegate that hold the reference of more than one method is called Multicast Delegates.To do multicast delegates we overload += operator. Multicast Delegates only have " void " return type.

using System;
public delegate int Delegate_Multicast(int a,int b);
public class ConceitCode
{
    
  public  static void Main(string[] args)
    {
        //Creating the Delegates Instance
        Delegate_Multicast delObjMulticast = new Delegate_Multicast(fn_Sub);
	delObjMulticast+=new Delegate_Multicast(fn_Mult);
	delObjMulticast(200,100);																	  																																				
    }

    static int  fn_Sub(int a,int b)
    {
	Console.WriteLine("Subtraction of 2 numbers using multicast delegates");
        int sub=a-b;
	Console.WriteLine(sub);
	return sub;
    }
    static int  fn_Mult(int x,int y)
    {
	Console.WriteLine("Multiplication of 2 numbers using multicast delegates");
        int mult=x*y;
	Console.WriteLine(mult);
	return mult;
    }
}

Output

Subtraction of 2 numbers using multicast delegates
100
Multiplication of 2 numbers using multicast delegates
20000		


Subscribe & Get Regular Updates On Your E-mail