C# has a data type that allows developers to pass methods as parameters of other methods. This data type is called a delegate. A delegate is a reference type.
Delegates are used to encapsulate and pass methods as parameters to other methods. A delegate can encapsulate a named or an anonymous method. You're most likely to use a delegate in events or callbacks.
Delegates are used to encapsulate and pass methods as parameters to other methods.
The delegate keyword is used to define a delegate type. A delegate type defines a method signature and when you create an instance of a delegate, you can associate with any method that has same signature. The method is invoked though the delegate instance.
Let's say, we have a method called AddNumbers that adds two numbers. The following code snippet defines the AddNumbers method that returns a sum of the two integer parameters.
public static int AddNumbers(int a, int b)
{
return a + b;
}
We want to use this method in a delegate.
We need to first ensure we define a delegate with the same signature. The following code snippet declares a delegate that has the same signature as the AddNumbers method.
public delegate int DelegateInt(int a, int b);
Now we need to define a delegate caller method that uses the preceding declared delegate as a parameter. The following code snippet defines a method that takes three parameters. The first two parameters are integer values and the third parameter is a delegate of type DelegateInt.
public static void DelegateCallerMethod(int a, int b, DelegateInt DelInt)
{
Console.WriteLine(DelInt(a, b));
}
Until now, our delegate has no idea of what it will be doing. All it knows is that it wraps a method that takes two integer values.
Now, to do the actual work, we must create an instance of the delegate by passing a method with similar signature.
The following code snippet creates a DelegateInt instance and passes the AddNumbers method. Now the delegate knows what method to execute when it is called.
DelegateInt dm = AddNumbers;
The following code is a call to the DelegateCallerMethod that takes a delegate as the third parameter that actually is the AddNumbers method.
DelegateCallerMethod(3, 6, dm);
You may not find a delegate useful everywhere but it is very important in events and callbacks when you want to force some events to be executed. Delegates also play a major role in LINQ.
The following code lists the complete sample.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DelegatesSample
{
class Program
{
static void Main(string[] args)
{
// Create a delegate and assign a method
DelegateInt dm = AddNumbers;
DelegateCallerMethod(3, 6, dm);
Console.ReadKey();
}
// Define a delegate. It has same signature as AddNumbers method
public delegate int DelegateInt(int a, int b);
// Method that will be used to assign to a delefate
public static int AddNumbers(int a, int b)
{
return a + b;
}
// Call delegate by passing delegate
public static void DelegateCallerMethod(int a, int b, DelegateInt DelInt)
{
Console.WriteLine(DelInt(a, b));
}
}
}
Summary
In this article, I explained the basics of delegates and how to use them in your code. Delegates are heavily used in events. Here is one detailed article on delegates and events.
Delegates and Events in C# and .NET