Understand Delegates Step by Step using C#
Definition of Delegate in C#:
Delegates are abstract pointer reference to methods or functions so that you can invoke in very generalize manner. It is also referred to as type safe method pointer. It helps to encapsulate the method name and calls it asynchronously
Syntax of a Delegate:
public delegate void DelegateMethod(); //Example 1 OR public delegate int DelegateMath(int x, int y); //Example 2
Note : Declare a delegate object with a signature that exactly matches the method signature that you are trying to encapsulate otherwise delegate will wont work if the delegate signature doesnt match with method.
So exactly matching of methods for both delegates will be.
public void MyMethod() //Example 1 } OR public int Math(int a, int b) return a + b;//Example 2 }
This is all about the syntax now lets go and try an understand creating and using the delegate in our code
Step By Step Creating and Using the Delegate
There are four steps invloded to create and to use the delegate as shown in below diagram
Using our (D C P I) protocol we will try to understand delegates
Step 1 Declare a Delegate
Before declaring a delegate let's create a class "clsMath" and simple functions as shown in below code.
Class claMath public int Add(int a, int b) return a + b; } public int Sub(int a, int b) return a - b; } }
Since the functions are declared so now let me create my delegate as same signature and same return type as my functions is.
public delegate int DelegateMath(int x, int y);
As you can see we have declared delegate "DelegateMath" with same return type (int) and same input parameters (int).
Step 2 Create a Delegate reference
DelegateMath objDelegateMath = null;
Above code we have created a delegate reference that is "objDelegateMath" and assigned it to "null".
Step 3 Point the reference pointer to methods
objDelegateMath = Add;//For Add Function objDelegateMath = Sub;//For Sub Function
Now we will create a new function with return type as my delegate I'm doing this because I want to call any one function at a time i.e. "Add" or "Sub" and will use the input pointer to call the any one function and I will also include delegate reference pointer to point both of our functions and return delegate reference with any one function pointed as per the input pointer as shown in below code.
public DelegateMath PointtoMath(int getpointer) DelegateMath objDelegateMath = null; if (getpointer == 1) objDelegateMath = Add; } else objDelegateMath = Sub; } return objDelegateMath; }
Step 4 Invoke the methods through delegate
objMaths.PointtoMath(1).Invoke(20, 20).ToString()//Outputs 40
To invoke the methods through the delegate we have created a main class of a program with main method in console application and inside the main class created the object for class "clsMath" and taken inputs for operation i.e. for "Add" or "Sub" and inputs for two numbers.
Finally we invoke the methods through a delegate by passing parameters to it as shown in below snippet of code.
static void Main(string args) clsMaths objMaths = new clsMaths(); Console.WriteLine("Please enter the operation code 1 for addition and 2 for substraction"); int opt = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("\nPlease enter the first number"); int num1 = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("\nPlease enter the second number"); int num2 = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("\nOutput answer is --> 0}",objMaths.PointtoMath(opt).Invoke(num1, num2).ToString()); }
Conclusion or Display Output Result
Hey friends if you have any doubts or queries kindly let me know through you valuable comments thank you.
By RANA on 2013-07-15Thank you. cool explanation. One quick question. The "Add" and "Sub" functions can be called even without using delegates. So could you please tell me the advantage we have on using delegates? And is there anything in C# which can be done "only using delegates"?
Add a Comment
Thank you, for commenting !!
Your comment is under moderation...
Keep reading other blogs