Hello Friends, In this tutorial we are going to learn Delegate in C#. A delegate is similar to a pointer in C++.  Let’s discuss it in detail with the example.

After Completing this tutorial you will be able to understand:

  • What is Delegate in C#?
  • Types of Delegate in C#?
  • Advantages of Delegate?

What is Delegate?

A delegate is a reference type Data Type that allows the programmer to encapsulates the reference to the method inside a Delegate object. A delegate is similar to Pointer to C and C++. The method that referenced the Delegate should have the same signature as that of Deleage.

Features of Delegate:

  1. A delegate is similar to Function Pointer in C++ but they are type safe.
  2. A delegate takes method as a parameter.
  3. A delegate can be used to define Callback method.
  4.  A delegate can be used as an Anonymous method. Anonymous methods are methods without method name and that contains inline code.
  5. Advantages of Delegate:
  6. Delegate hide the method call’s from the user.
  7. Effective use of Delegate can increase the performance of the application.
  8. Delegate used to call a method asynchronously.

There are few steps of using Delegate:

Step 1- Instantiating Delegate:

Syntax:



Access modifier delegate return type DelegateName ([arguments list]);

Example:



public delegate void delCalc(int iNumber1,int iNumber2);

While declaring a delegate the access modifier, return type and the number of arguments and data type should be same.

Step 2- Instantiating a Delegate:

Syntax:



DelegateName objDelegate=new DelegateName(FunctionName)l

Example:

Consider a Function:



Public void Add(int iNumber1,int inNumber2)



{



Console.WriteLine(“Addition of Number {0}”,iNumber1+iNumber2);



}



delCalc objDelegate=new delCalc(Add);

Step 3- Invoking a Delegate:

Syntax:



Delegate Object Name([Argument List]);

Example:



objDelegate(10,20);

Types of Delegate:

1-Single Cast Delegate:

Single Cast Delegate holds the reference of only one method.

An example of Single Cast Delegate:

Create a Console Application called SingleCastDelegateDemo.

Delegate

Open Program.cs file and write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace SingleCastDelegateDemo
{
delegate int DELSimpleCalculator(int iFirstNumber, int iSecondNumber);
class Program
{
public static int AddNumber(int iFirstNumber, int iSecindNumber)
{
return iFirstNumber + iSecindNumber;
}
public static int SubtractNumber(int iFirstNumber, int iSecindNumber)
{
return iFirstNumber - iSecindNumber;
}
public static int MultiplyNumber(int iFirstNumber, int iSecindNumber)
{
return iFirstNumber * iSecindNumber;
}
public static int DivideNumber(int iFirstNumber, int iSecindNumber)
{
return iFirstNumber / iSecindNumber;
}
static void Main(string[] args)
{
Program objProgram = new Program();
DELSimpleCalculator objDelegate1 = new DELSimpleCalculator(AddNumber);
DELSimpleCalculator objDelegate2 = new DELSimpleCalculator(SubtractNumber);
DELSimpleCalculator objDelegate3 = new DELSimpleCalculator(MultiplyNumber);
DELSimpleCalculator objDelegate4 = new DELSimpleCalculator(DivideNumber);
Console.WriteLine("Addition of Number {0}", objDelegate1(10, 5));
Console.WriteLine("Subtraction of Number {0}", objDelegate2(10, 5));
Console.WriteLine("Multiplication of Number {0}", objDelegate3(10, 5));
Console.WriteLine("Division of Number {0}", objDelegate4(10, 5));
Console.ReadKey();



}
}
}

Run the application and look over the output window:

Delegate

2- Multicast Delegate:

 Multicast Delegate holds the reference of multiple method.

An example of Multicast Delegate:

Delegate

Open Program.cs file and write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace MulticastDelegateDemo
{
delegate void DELSimpleCalculator(int iFirstNumber, int iSecondNumber);
class Program
{
public static void AddNumber(int iFirstNumber, int iSecindNumber)
{
Console.WriteLine("Addition of Number {0}", iFirstNumber + iSecindNumber);
}
public static void SubtractNumber(int iFirstNumber, int iSecindNumber)
{
Console.WriteLine("Subtraction of Number {0}", iFirstNumber - iSecindNumber);
}
public static void MultiplyNumber(int iFirstNumber, int iSecindNumber)
{
Console.WriteLine("Multiplication of Number {0}", iFirstNumber * iSecindNumber);
}
public static void DivideNumber(int iFirstNumber, int iSecindNumber)
{
Console.WriteLine("Multiplication of Number {0}", iFirstNumber / iSecindNumber);
}
static void Main(string[] args)
{
DELSimpleCalculator objDelegate = new DELSimpleCalculator(AddNumber);
objDelegate += new DELSimpleCalculator(SubtractNumber);
objDelegate += new DELSimpleCalculator(MultiplyNumber);
objDelegate += new DELSimpleCalculator(DivideNumber);
Console.WriteLine("First Call to Delegate");
objDelegate(10, 5);//AddNumber,SubtractNumber, MultiplyNumber and DiviideNumber method will be called

objDelegate -= new DELSimpleCalculator(DivideNumber);
Console.WriteLine("Second Call to Delegate");
objDelegate(15, 5);//AddNumber,SubtractNumber, MultiplyNumber method will be called
Console.ReadKey();



}
}
}

In the above example, you can see we have added the reference to multiple methods using += operator. You can also remove reference to the method using -=.

Run the application and look over the output window.

Delegate

Conclusion:

I hope you are more clear about Delegate. I would like to have your Comments, Suggestions and Feedback.

View More:

Thank You.

Leave a Comment