This article helps you to understand the basics of delegate in the .NET Framework.
What is a Delegate?
In the C/C++ languages, function pointers are a buzzword; they can be used to point to a function and call the pointer in order to invoke the function. In .Net, there is nothing called pointer so .Net has delegated. In one sentence, a delegate is a glorified pointer to a function. You can call a static or instance method (function) using traditional classname.methodname or just pointing the method to delegate and call the delegate which, in turn, will call the method.
Delegates provide a mechanism for defining and executing callbacks. This allows you to define the exact signature of a callback and that defines the type of delegate. In general, delegate instances consist of fields like a reference to an object and a pointer to an instance method. However, if the object reference is null, CLR understands it as a static method.
When you declare a delegate, the C# compiler generates a class derived from MulticastDelegate. MulticastDelegate contains several methods but you cannot see those methods if you view the IL of your code in ILDASM. It is because CLR implements all these methods dynamically at runtime.
Delegate allows classes to not be coupled strongly. Also, the caller of the delegate has no idea whether it is calling a static method or instance method or what exact method it is calling. Think about the GUI Button class. Button class doesn't know which method it will execute onClick event. Just think, if we write the code within the button class OnClick event, then would it be a reusable class? Button class publishes the OnClick event using a delegate and specifies that you can subscribe to the OnClick event with a method. In this way, you can de-couple your classes using a delegate.
How to call Delegate
Before delving into code, you need to understand one very simple rule about delegates. The return-type or parameter list of the method that you want to reference by the delegate should match with the delegate type declaration. Confused! Take an example
Suppose I have one method like void PrintToConsole(string message). The delegate type should be delegate void objDelegate(string message). Now if you write objDelegate = PrintToConsole then it is perfect. Call the function using objDelegate("Hello World") which will execute the PrintToConsole method.
So what does the following statement mean?
- public delegate void _delegate(int i, int j);
It means a declaration of delegate that can encapsulate any method which will take two integer parameters and return void.
When you declare the above line, the C# compiler produces the following code snippet
- public class NotifyGpaDelegate: System.MulticastDelegate {
- public void Invoke(int GPAScore) {
- // code
- }
- }
This Invoke method is part of MulticastDelegate and should have the same signature as the defined delegate.
Simple Delegate
- public delegate void _MathDelegate(int x, int y);
- public class clsMath {
- public void Sum(int i, int j) {
- Console.WriteLine(i + j);
- }
- public static void Subtract(int i, int j) {
- Console.WriteLine(i - j);
- }
- }
- class Program {
- static void Main(string[] args) {
- clsMath _math = new clsMath();
- _MathDelegate _delegate1, _delegate2;
- _delegate1 = _math.Sum;
- _delegate2 = clsMath.Subtract;
- _delegate1(100, 50);
- _delegate2(100, 50);
- }
- }
In the above code, I defined a delegate that returns void and takes two integer parameters. Since I want to call the Sum/Subtract method using this delegate, you can see I have made the signature compatible between delegate and Sum/Subtract method. In my main method, I declared the delegates, referenced the delegates with the methods, and called the delegates, which in turn calls the methods. This is an example of a simple delegate
Delegate Chaining
It means you can maintain a list of delegates which can be called. Internally it maintains a linked list of delegates and when the head of the list is called, all the delegates in the chain are called. In the Delegate class, the following methods help to form a delegate chain.
- public abstract class Delegate : ICloneable, ISerializable
- {
- public static Delegate Combine(params Delegate[] delegates);
- public static Delegate Combine(Delegate a, Delegate b);
- }
The first method takes an array of delegates and returns a delegate type. You can also remove the delegate from the delegate chain using the following methods defined in the Delegate class
- public abstract class Delegate : ICloneable, ISerializable
- {
- public static Delegate Remove(Delegate source, Delegate value);
- public static Delegate RemoveAll(Delegate source, Delegate value);
- }
The source is the delegate chain and value is the delegate that you want to remove from the delegate chain.
In order to make programming simple, the C# compiler uses operator overloading to implement these methods. You can use + to combine and – to remove from the delegate chain.

clement rajPosted Dec 16, 2010, 5:00 AM
nice work........
knightsPosted Dec 14, 2010, 6:44 PM
It is a nice effort and i would like to add something that wouldnt it be nice if we can have better examples rather adding or subtracting since these are pretty generic and more often than not, now a days its always easy to reffer to a real time scenario where you end up using delegates, which can enlighten the implimentation and usage of delegates rather their concept.Give us another good thing like above if at all possible, keeping in mind current implimentations.Thank you.
Suthish NairPosted Dec 14, 2010, 9:08 AM
good one
Manikavelu VelayuthamPosted Dec 14, 2010, 6:43 AM
Top to Bottom of Delegates is Analysed here. Really a good one.