Delegates in C#

"delegate" is a CLI (.Net) class. It is in the namespace System.Delegate. A delegate is a function pointer and the address of that pointer is in RAM and that address is not accessible anywhere else.

Let's briefly go through it.

Suppose we have a caller M(); and a method; void M(){}. Now the caller must call this method but it wants to call the function later on. So basically the caller wants someone that can hold the identity (address of the functions to be called) .

Here comes the privilege role of delegates. Now what happens is that delegates just sit between the caller and the method and holds the address (the name and in which class) of the functions. So what we simply do here is make an object of the delegate class and then request the delegate to call the function whenever we want them to be called.

So the benefits of delegates are that we need not be interested in the identity of the function, in other words the name and in which class they exist. We just assign them the address of the function.

For example, setting up an alarm clock. Suppose we want to set an alarm of 6 A.M. in the morning. so here we will use delegates. We will simply declare a delegate type and assign it the address of the alarm clock. Now it is the responsibility of the delegate to search for the identity of the alarm and call it exactly at 6 A.M.

Other examples visiting card:
  • C# Corner news letter.
  • Stock price broker.
Now some may think when the caller and method both are ready to communicate then what is the necessity of using delegates. Delegates basically make our code secure because the code of the function that we are calling can also be written by someone else, hence we just provide a reference of the function using delegates.

So delegates act as a layer of abstraction.

Delegates can be given the address of more than one function but it will call them in the order in which we have added them and the list of all the methods whose address is assigned to delegates is called an "INVOCATION LIST".
 
Also note that using a delegate runs the risk of someone else attaching some more functions that are not intended to be called, that's why access modifiers are important so that anyone else cannot access them.

Also the delegate type should be given the same method signature as the method to be called.

If we assign the reference of all functions on a single variable then only the last function will execute that we took in the binding stage.

We call a single method but implicitly it will call all the methods that have its reference at the binding stage.

The (-=) operator can be used to remove a method from the invocation list.

So we can list the following 4 points about delegates from the preceding discussion: 
  1. Encapsulated with an object of a class.

  2. Type safe, in other words are not applicable to invalid memory addresses.

  3. Strongly typed, in other words can only point to methods whose signatures are known to us.

  4. Implicitly derived from Multicast delegates.

Now we will see step-by-step how to create and use delegates.

Step 1: Write the method that the delegate will point to.

  1. static int PrintString(string s)  
  2. {  
  3.    Console.Writeline(s);  
  4.    return string.is NullorEmpty(s)?0:s.Length;  
  5. }  
Step 2: Define the delegate type.
  1. delegate int StringAction(string s)  
Note that it looks as if we are declaring a method but actually we are declaring a class that is implicitly derived from multicast delegate.

Step 3: Create delegate instance.

Step 4: Wire delegate instance with the method in Step 1.
  1. StringAction stringAction= new StringAction(Program.PrintString);  
Step 5: Invoke the method using delegate.
  1. StringAction('Hello World!");  
Now join all these procedures along with the main method in a new project in Visual Studio you will get the output "Hello World" on the console.

I have tried my best to briefly describe delegates. Still if anyone has queries regarding this article, please do comment.


Similar Articles