Introduction
Here we will see the BP of Delegates in C#.Net,
1. Basics of delegates
2. Power of delegates
Basics of delegates covers:
1. what is a delegate?
2. how to create a delegate?
3. what are the types of delegates?
Power of delegates covers
2. delegates as function pointers
Background
we set up a simple console application before we start with delegates, so please create a console application and write the code as shown below
- class Program
- {
- static void Add()
- {
- int num1=90;
- int num2=10;
- Console.WriteLine("Sum:"+(num1+num2));
- }
- static void Main()
- {
- Add();
- Console.ReadLine();
- }
- }
Basics of delegates
1.what is a delegate?
meaning: delegate is a person who is authorized to represent others
1. delegate is a representative of method(according to the meaning)
2. delegates are like function pointers in C/C++ but are type safe(from msdn).
2.how to create a delegate?
we use "delegate" keyword to create delegate
we have to follow 3 steps to create a delegate
1. 1.declare a delegate
2. instantiate a delegate
3. invoke a delegate
1.declare a delegate: delegate can be declared inside the namespace or inside the class
do you know declaring abstract method, then it's easy to declare delegate as well
- syntax: [access modifier] delegate return_type delegate_name([parameters])
- example: public delegate void AddMethodHandler();
2.instantiate a delegate: it is the process of creating an object to delegate, which is similar to creating an object to a class with a single parameterized constructor.
- AddMethodHandler objAddDel=new AddMethodHandler(Add);
as we have instance class and static class,delegates also can be instance delegate and static delegate, so we have to instantiate a non static delegate.
3.invoke a delegate: it is the process of calling a delegate which is similar to method calling
- objAddDel();
3.What are the types of delegates?
delegates are 2 types
1. singlecast delegate
2. multicast delegate
1.siglecast delegate: if a delegate represents a single method then its called singlecast delegate, here we create a delegate that represents a single method
- //step1: declare a delegate
- public delegate void AddMethodHandler();
- class Program
- {
- static void Add()
- {
- int num1=90;
- int num2=10;
- Console.WriteLine("Sum:"+(num1+num2));
- }
- static void Main()
- {
- //step2:instantiate a delegate
- AddMethodHandler objAddDel=new AddMethodHandler(Add);
- //step3:invoke a delegate
- objAddDel();
- Console.ReadLine();
- }
- }
if we execute and test the application, we can observe that the Add method gets executed,hence we can say that delegate is representing method.
Note: return type and parameters of both(method and delegate) must be same.
2.multicast delegate: if a delegate represents two or more methods then it is called multicast delegate, we see how a single delegate represents two methods
- //step1: declaring a delegate inside namespace
- public delegate void AddMethodHandler();
- class Program
- {
- static void Add1()
- {
- double num1=90;
- double num2=10;
- Console.WriteLine("Sum:"+(num1+num2));
- }
- static void Add()
- {
- int num1=90;
- int num2=10;
- Console.WriteLine("Sum:"+(num1+num2));
- }
- static void Main()
- {
- //step2:instantiate a delegate
- AddMethodHandler objAddDel=new AddMethodHandler(Add);
- //make a delegate as multicast
- objAddDel+=Add1;
- //step3:invoke a delegate
- objAddDel();
- Console.ReadLine();
- }
- }
execute the above code and we can observe the execution of two methods. now we done with basics of delegates
Power of delegates
power of any feature lies in it's irreplacable situation(a situation where we can not proceed without the feature), so we see the following 2 situations are really very important to understand the power of delegates.
1.delegates as event handlers
Let’s understand with practical example, hope you create at least a single button in asp.net web application/ windows forms applications as shown below
We know that when we/user clicks on Submit button then btnSubmit_Click method gets executed, that’s correct but still there are 2 more things involved in between button and method. Those 2 things are
1. Event
2. Delegate
Now will see code flow and try to understand this relationship
Now we can understand that button control cannot react to the user interaction by invoking appropriate method without the help of Event(Click) and Delegate(EventHandler).
Note: EventHandler is a delegate created by Microsoft, now we have seen that delegate as an event handler.
2.delegates as function pointers
all of us know that usually we pass values as parameters to a method, assume in case if we have to pass method as parameter !
This is possible with function pointers in C/C++, we use delegate to achieve the same functionality in C#.Net
1.create a callback method: method with parameter(parameter is method)
2.pass method as parameter: pass a method as parameter to callback method
- class Program
- {
- //declaring a delegate inside a class
- public delegate void AddMethodHandler();
- static void Add()
- {
- int num1=90;
- int num2=10;
- Console.WriteLine("Sum:"+(num1+num2));
- }
- static void Main()
- {
- //calling callback method
- Calculate(Add);
- Console.ReadLine();
- }
- //callback method:a method that receives method as a parameter
- static void Calculate(AddMethodHandler objAddDel)
- {
- objAddDel();
- }
- }
Execute and test the output, we have learnt here delegate as a function pointer.
Well, we have learnt passing one type of method as parameter,what about rest of the types of methods?
Types of methods:
- without return type-without parameters
- without return type-with parameters
- with return type-without parameters
- with return type-with parameters
To represent these all types of methods Microsoft provided us delegates,it means that we need not to declare any delegate, directly use them for your work!
1. without return type-without parameters: to represent this type of methods we have a delegate with name “Action” with the following declaration.
- public delegate void Action();
Example using Action:
- class Program
- {
- static void Add()
- {
- int num1=90;
- int num2=10;
- Console.WriteLine("Sum:"+(num1+num2));
- }
- static void Main()
- {
- Calculate(Add);
- Console.ReadLine();
- }
- static void Calculate(Action objAddDel)
- {
- objAddDel();
- }
- }
2. without return type-with parameters: to handle this type of methods, we have 16 overloaded generic Action delegates.
Example using Action<in T1,int T2>:
- class Program
- {
- //without return type-with parameters
- static void Sub(int num1,int num2)
- {
- Console.WriteLine("Diff:"+(num1-num2));
- }
- static void Main()
- {
- Calculate(Sub);
- Console.ReadLine();
- }
- static void Calculate(Action<int,int> objSubDel)
- {
- objSubDel(90,10);
- }
- }
Note: Action<> delegate allows maximum of 16 parameters( all are input parameters only).
3.with return type-without parameters: Func delegate is used to handle these type of delegates.
- public delegate TResult Func<out TResult>();
Example using Func<out TResult>:
- class Program
- {
- //without return type-without parameters
- static int Div()
- {
- int num1=90;
- int num2=10;
- return num1/num2;
- }
- static void Main()
- {
- Calculate(Div);
- Console.ReadLine();
- }
- static void Calculate(Func<int> objDivDel)
- {
- int divResut=objDivDel();
- Console.WriteLine("Div:"+divResult);
- }
- }
4.with return type-with parameters: Func overloaded delegates with input and output parameters are used to represent these type of methods.
Example:
- class Program
- {
- //without return type-with parameters
- static int Mul(int num1,int num2)
- {
- return num1/num2;
- }
- static void Main()
- {
- Calculate(Mul);
- Console.ReadLine();
- }
- static void Calculate(Func<int,int,int> objMulDel)
- {
- int mulResut=objMulDel();
- Console.WriteLine("Mul:"+mulResult);
- }
- }
Note: Func<> delegate allows maximum of 16 input parameters out of 17 parameters, always the last parameter is out parameter.in case if it has only one parameter, then it is out parameter only.
Well done, we have seen all types of methods and available delegates, but still I want to introduce one more delegate which handle the following type of method.
boolean return type-single parameter:
- public delegate bool Predicate<in T>(T obj);
some times we have to return true/false from the method based on provided input,this delegate is useful in those scenarios.
Example:
- class Program
- {
- //with bool return type-with parameter
- static bool IsUpperCase(string value)
- {
- return value.Equals(value.ToUpper());
- }
- static void Main()
- {
- Test(IsUpperCase);
- Console.ReadLine();
- }
- static void Test(Predicate<string> obj)
- {
- bool result = obj("Hi");
- Console.WriteLine(result);
- }
- }
Here is the conclusion diagram for you
That's it for the article,please provide your valuable comments and feedback.

Gowtham KPosted Dec 18, 2015, 8:18 AM
Good One
Ankur MistryPosted Dec 18, 2015, 5:33 AM
good start
Humayun Kabir MamunPosted Dec 18, 2015, 4:45 AM
Nice...