Delegates in C# is nothing but a callback mechanism that allows a type safe pointer function capabilities than the legacy function pointer in C/C++.

What is function pointer in legacy program ?

If you are familiar with legacy programs like C/C++, then you must be knowing about a concept as function pointer“. This function pointer variable allows to hold memory address of a method, this variable then can be passed to any other method as an argument or can be called inside another method.

The variable only holds memory address and doesn’t carry any information like number of parameters that function expects, data type information of these parameters and function’s return type etc. which is why this function pointers are not type safe.

Delegate definition in C#

In C#, Delegates brings similar concept of function pointer which are simple and type safe. Delegates in C# are type-safe function pointers. that have the ability to call single method or multiple methods sequentially.

This is what Microsoft says about delegate.

delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.

How to declare a delegate in C# ?

A delegate refers to a method, which has the same signature and arguments as that of the delegate. Following is the declaration pattern.

delegate [return type] [delegate name] [argument list]

  • delegate: Keyword that is used to define the delegate.
  • return type: The type of value returned by the method which the delegate going to call, it can be void. 
  • delegate name: It is the user-defined name or identifier for the delegate.
  • argument list: This is the list of arguments , which are required by the method while calling through delegates.

For example, we have declared a delegate as follows.

This above delegate TemperatureConverter holds reference of any method that has return type as double and accepts a single in-put parameter of type double. Here in above this delegate TemperatureConverter points to any of below method.

Delegate instantiation in C#

In order to instantiate a delegate object, you need a method with parameter and return type that matches the delegate type itself. In below,

  • We have create a delegate object as ConvertTemperature delTempConv;
  • We instantiate the delegate object using new keyword and then assigned the method ConvertTempCelToFahrenheit , that has same parameter type and return type of the delegate.

The delegate object delTempConv holds the reference of the method ConvertTempCelToFahrenheit . Now, we can pass this delegate object to any other method as a parameter or can invoke it separately. In below, we are invoking the delegate object by passing required parameter.

If you run the the code in debugger mode and check at the delegate object delTempConv in the Visual Studio Quick Watch window, then you can see that the delegate object holds all information like, number of parameters that function expects, data type information of parameters and function’s return type etc.

Delegate Type OutPut 1.0

Here in above, the C# delegate object holds reference of the method, return types and parameter details as well, which makes delegate type as type safe.

Example of delegate declaration and invocation in C# :

Following is the complete example of delegate.

When we run above source code, then below output will be generated.

Delegate Type OutPut 1.1

Thanks for your invaluable time 🙂 I appreciate it, keep visiting and do check my other article on TryParse() vs Parse() in C#