In this blog we will discuss about Delegate feature in C#. When we talk about Delegates, then one thing comes in to our mind is about Function pointers in legacy program. So  lets understand little bit about function pointers.

What is function pointer in legacy program ?

If you are familiar with legacy programs like C/C++, then there is a concept as function pointer“. This function pointer is a variable, that holds memory address of a method, so generally you have a variable pointing to a function.

This variable as any other variable can be passed to other method as an argument. So typically you are passing a method variable in to another method and invoking that it inside other method.

In case of legacy program, the pointer variable only holds memory address and doesn’t carry any information , e.a number of parameters that function expects, data type information of these parameters and function’s return type etc. This behavior of function pointer makes it as unsafe.

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 delegates 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. This delegate TemperatureConverter points to any of below method.

How to instantiate delegates 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.

How delegates are type safe ?

If you run above code in debugger mode and check at the delegate object delTempConv in the Visual Studio Quick Watch window, then you will see the delegate object and its properties in detail.

Delegate Type OutPut 1.0

Here in above, the C# delegate object has properties like ReturnParameter, ReturnType etc , which has all type information of method that the delegate is pointing to. So when you invoke a delegate type, if there is a mismatch in type information then it throws compile time error.

Example of delegate declaration and invocation in C# :

Following is the complete example of delegate.

When we run above source code, then it generates below output.

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#