Delegates in C#

This article describes an introduction of C# delegates;basic overview of C# delegates .Summary of the article:

  • What is Delegates?
  • Declaring Delegates
  • Properties of Delegates
  • Multicasting of a Delegate


What is Delegates?
In general delegate is someone who speaks or acts on behalf of an organization at a meeting or conference between organizations of the same level. In C# delegates are similar to function pointer of C/C++. Delegate is a reference type variable that holds the reference to a method and the reference can be changed at runtime.
Generally delegates are used in the following cases:

  • Implementation of design patterns
  • Event handlers
  • Callbacks
  • LINQ

There is nothing that is done with delegates that cannot be done with our regular methods. Delegates are used because they bring numerous advantages. They provide flexibility of the application and code reuse. Like interfaces delegates generalize our code. It also allows methods to be passed as parameters. We use delegate, when we need to decide which method to call at runtime. Finally, delegates provide a way of specializing behavior of a class without sub classing it. Classes may have complex generic behavior but are still meant to be specialized. Classes are specialized either via delegates or through inheritance.


Declaring Delegates
Delegate declarations determine the methods that can be referenced by the delegate. A delegate can refer to a method which has the same signature as that of the delegate.
For example consider a delegate:

public delegate int MyDelegates(string a, int b);

The above delegate can be used to reference any method that has one string and one int type parameter and returns an int type variable.

Syntax for delegate declaration is:
delegate <return type> <delegate-name> <parameter list>

Example of C# Delegates
For an example of delegates in C# consider the following console application:

delegate int Calculator(int n);

public static int AddNumber(int a)
    return a + 10;

public static int MulNumber(int a)
    return a * 10;

static void Main(string[] args)
    Calculator oCalculator1 = new Calculator(AddNumber);
    Calculator oCalculator2 = new Calculator(MulNumber);

    Console.WriteLine("Result of Addition      : {0}", oCalculator1(50));
    Console.WriteLine("Result of Multiplication: {0}", oCalculator2(5));

Result of Addition : 60
Result of Multiplication: 50

Properties of Delegates
Delegates have the following properties:

  • It is similar to C++ function pointers.
  • Its allow methods to be passed as parameters.
  • It can be used to define callback methods.
  • It can be chained together; for example, multiple methods can be called on a single event.
  • Methods don’t need to match the delegate signature exactly.
  • C# version 2.0 introduces the concept of Anonymous Methods. It permits code blocks to be passed as parameters in place of a separately defined method.


Multicasting of a Delegate
A Delegate objects can be composed using the “+” operator. This composed delegate calls the two delegates it was composed from. Only the delegates of the same type can be composed. The “-” operator is used to remove a component delegate from a composed delegate.
Using this useful property of delegates we can create an invocation list of methods that will be called when a delegate is invoked. This is called delegate multicasting or multicasting of a delegate.
It holds the reference of more than one method. It must contain only methods that return void. Otherwise it will through run-time exception.
Consider the multicasting of a delegate:

delegate void DelegateMulticast(int x, int y);
Class Class2
static void Method1(int x, int y)
Console.WriteLine(“You r in Method 1”);

static void Method2(int x, int y)
Console.WriteLine(“You r in Method 2″);

public static void <place w:st=”on” />Main</place />()
DelegateMulticast objdm = new DelegateMulticast(Method1);
objdm += new DelegateMulticast(Method2);
objdm(1,2); // Method1 and Method2 are called
objdm -= new DelegateMulticast(Method1);
objdm(2,3); // Only Method2 is called

In the above example, two methods are defined named method1 and method2 which take two integer parameters and return type as void.


  1. By Malek Aktar


Leave a Reply

Your email address will not be published. Required fields are marked *