Real Use Of Delegates In C#

Introduction

 
In this post, we will talk about delegates and their real use. Usually, in an interview, people will say a delegate is nothing but a pointer to a function. When looking at this definition, it looks very stupid.
 

What is a delegate?

 
Let’s discuss it with a simple program when we say a delegate is a pointer to a function.
  1. class program {  
  2.     public delegate void somemethodptr()  
  3.     static void Main(string[] args) {  
  4.         somemethodptr obj = new somemethodptr(somemethod);  
  5.         obj.Invoke();  
  6.     }  
  7.     static void somemethod() {  
  8.         console.WriteLine(“Method called”);  
  9.     }  
  10. }  
In the above program, I am creating a delegate by creating an object pointing to a method. if I run the above program it will work without any issues, but does it make sense? So now the question is a method can call directly what is the use of calling indirectly? so in order to understand delegate really check in the dictionary what the meaning of delegate word is delegate is kind of representation. The main goal of the representator is to communicate between two parties.
 
The actual definition in C# delegate is meant to do communication between two things. Let me explain what it is a delegate.
 
Now assume for example you have class myclass this class having long-running method. I am calling myclass in the main method. Now we have two parties one party is the main program another one myclass. 
 
Let us say first-party wants to get information on which number is running for a loop. It means the first party wants to get live updates.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Delegateexample  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             myclass obj = new myclass();  
  14.             obj.longrunningmethod(callback);  
  15.   
  16.         }  
  17.   
  18.         static void callback(int i)  
  19.         {  
  20.             Console.WriteLine(i);   
  21.         }  
  22.         public class myclass  
  23.         {  
  24.             public delegate void callback(int i);  
  25.             public void longrunningmethod( callback obj)  
  26.             {  
  27.                 for (int i = 0; i <= 100; i++)  
  28.                 {  
  29.                     // does something  
  30.                     obj(i);  
  31.                 }  
  32.   
  33.             }  
  34.   
  35.   
  36.         }  
  37.     }  
  38. }  
In the below program, what we are done is myclass is saying send me to delegate via call back method and it ensues I will make a callback method with the current number. In other words, myclass is communicating via delegate, In other words, the use of delegates is callbacks.
 

Summary

 
In this blog, we have discussed the real use of delegate in C# with a simple program.


Next Recommended Reading Delegates With A Real Time Example In C#