C# Delegates for Beginners

Delegate is a type that can reference to a method. once delegate is assigned to a method it can behave exactly like that method. so now I'm going to show you a simple example to demonstrate how delegate is work.  so let's go to the code and check that one. I'll explain things while i show you the code. okay.

To demonstrate this i created a console application named demo. now we need a simple class to add our calculation methods. so go to your project and create a class name Calculation and add following method to your class

 1. Calculation Class
 /// <summary>  
   /// Calculation Class  
   /// </summary>  
   public class Calculation  
   {  
     #region Public Methods  
     /// <summary>  
     /// Adds two values  
     /// </summary>  
     /// <param name="value1">The value1.</param>  
     /// <param name="value2">The value2.</param>  
     /// <returns></returns>  
     public int AddTwoValues(int value1, int value2)  
     {  
       return value1 + value2;  
     }  
     /// <summary>  
     /// Multiplies the two values.  
     /// </summary>  
     /// <param name="value1">The value1.</param>  
     /// <param name="value2">The value2.</param>  
     /// <returns></returns>  
     public int MultiplyTwoValues(int value1, int value2)  
     {  
       return value1 * value2;  
     }   
     #endregion  
   }  

Then goto your Program.cs and create a delegate call CalculationHandler to handle Methods in Calculation class. An important thing you should know about delegate is delegate's method signature always equal to the methods you are going to assign to the delegate. in our case delegate's return type should be integer and it should take to integer parameters. so our delegate is look like this
     /// <summary>  
     /// Delegate to Handle Calculation Class Methods  
     /// </summary>  
     /// <param name="x">The x value</param>  
     /// <param name="y">The y value</param>  
     /// <returns></returns>  
     public delegate int CalculationHandler(int x, int y);  

Now let's go and see how to assign a method to delegate and how it works.

2. Program.cs

 internal class Program  
   {  
     /// <summary>  
     /// Delegate to Handle Calculation Class Methods  
     /// </summary>  
     /// <param name="x">The x value</param>  
     /// <param name="y">The y value</param>  
     /// <returns></returns>  
     public delegate int CalculationHandler(int x, int y);  
   
     #region Methods  
   
     /// <summary>  
     /// Mains the specified arguments.  
     /// </summary>  
     private static void Main()  
     {  
       //Create a new Instance of Calculation Class  
       var calculationObj = new Calculation();  
   
       //create a new instance of the delegate class to handle Add Method  
       CalculationHandler addHandler = calculationObj.AddTwoValues;  
       var result = addHandler(3, 4);  
   
       //create a new instance of the delegate class to handle Multiply Method  
       CalculationHandler multiplyHandler = calculationObj.MultiplyTwoValues;  
       var result2 = multiplyHandler(3, 4);  
   
   
       Console.WriteLine("Result of the Add Method");  
       Console.WriteLine(result);  
       Console.WriteLine("Result of the Multiply Method");  
       Console.WriteLine(result2);  
       Console.ReadLine();  
     }  
     #endregion  
   }  

Now you can see our delegate method get the results of AddTwoValues and MultiplyTwoValues method because the method signatures are same. likewise it can call to any other method if the method signature is equal to the delegate method signature. so the final output of the main method is something like this.





















one of the advantage of the this delegate is we can implement callback methods. I'll explain about the callback methods in another article. Have a nice day and happy coding...!

Comments

Popular posts from this blog

Deep dive into OpenXML Part 1

Deep dive into OpenXML Part 2

Convert XML data to dynamic (ExpandoObject) Object