Understanding About Delegates In C#

We have often heard about delegate but, what is it? why do we need it? how to use it? Let’s try finding answers to these questions.

What is delegate?

In simple language, It is called pointer to a function. Delegates are used to pass methods as arguments to other methods.

It 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.

Event Handlers are nothing more than methods that are invoked through delegates. You can create a custom method, and a class such as a windows control can call your method when a certain event occurs.

Why we need delegate?

  • Delegates allow to pass method 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 have to match the delegate type exactly.

How to use it?

So far I have mentioned pretty much what you all have seen everywhere. Let's try to understand with an example:

Understanding about Delegates in C#

In the above example, I have taken a LongRunningClass which has LongRunningFunction performing some long running task. Now another class member function is requesting LongRunningFunction to start the action and pass the information. To achieve this first we shall define a delegate in the LongRunningClass, and requesting to pass the information from another class by passing method as a parameter.

Types Of Delegate

  • Single Delegate
  • Multicast Delegate
  • Generic Delegate
    • Func Delegate
    • Action Delegate
    • Predicate Delegate

Single Delegate

It is used to invoke single methods.

Understanding about Delegates in C#

Multicast Delegate

It is used to invoke multiple methods.

Points to note:

  • All the methods will be invoked in the same order as it is added.
  • If the delegate has return type other than void then the returned value of the last invoked method will be returned.
  • If the delegate has an out parameter then the value of the out parameter will be the value assigned by the last invoked method.

Understanding about Delegates in C#

Another approach to create Multicast Delegate

Understanding about Delegates in C#

Generic Delegate

It was introduced in .Net 3.5 so we don't need to define the delegate instance in order to invoke a methods

  • Func Delegate
  • Action Delegate
  • Predicate Delegate 

Func Delegate

  • It is a generic delegate which is defined in the System namespace.
  • Func delegate must return a value.
  • Func delegate can have 0 to 16 input parameters.
  • Func delegate does not allow ref and out parameters.
  • Func delegate must have 1 out parameter for the result.

Action Delegate

  • Action delegate is same as Func delegate type except that it does not return anything.
  • Action delegate does not return any value.
  • Action delegate can have 0 to 16 input parameters.
  • Action delegate does not allow ref and out parameters.
  • It can be used with Anonymous method and Lambda Expressioin.

Predicate Delegate

  • Predicate is same like Func and Action delegate type except that It must take one input parameter and return boolean type.
  • Predicate delegate takes one input parameter and boolean return type.
  • Anonymous method and Lambda Expressioin can be assigned to Predicate delegate


Similar Articles