Hi... Today's topic is very interesting in C#. Some says its very complex. Guess it ... yes, you are right. Topic is Delegate. I hope you have worked on delegate but who is fresher, it is a very complex topic, So, I am going to explain and after that I hope you will know about delegate and love to use delegate in project.
What is Delegate?
If you have got a chance to work with C++ or you will from C++ background then you definitely know about function pointer in C++.
Delegate is as like as function pointer in C# but delegates are type safe. Delegate is an object that point to another method and method can be static or instance. It take arguments and return value. It takes reference to other methods. We can invoke or call the method through the delegate object.
Point to remember regarding Delegate
1. Using Delegates we can pass methods ad parameters.
2. Using Delegate we can define callback methods.
3. We can call multiple methods on a single call using Delegate.
To use Delegate, there are only four steps you to do
1. Declare a delegate type.
2. Create or Find a method which has same type of delegate.
3. Create object/instance of Delegate.
4. And at last Invoke Delegate object.
Declare a Delegate type
First step to start with delegate is to declare the delegate type. The “delegate” keyword is used to define a delegate type.
//Declare a Delegate
delegate int NextProgrammingDelegate(int x, int y, int z);
//Declare a Generic Delegate
public delegate T Add<T>(T x, T y, T z);
Create or Find a method which has same signature as Delegate
To use delegate you have methods or to create methods which have same signature of delegate. We can use instance and static methods with delegate.
Create instance of Delegate
//For simple delegate
NextProgrammingDelegate objDelegate1 = new NextProgrammingDelegate(DelegateExample.Average);
//For Generic type delegate
Add<double> objDoubleTypeDelegate = GenericDelegateExample.AddDouble;
Invoke Delegate object
//First Way to call delegate
Console.WriteLine("value is " + objDelegate1.Invoke(3, 8, 9));
//Second Way to call delegate
Console.WriteLine("value is " + objDelegate1(3, 4, 5));
Full Example of Simple Delegate
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NextWithDeleage
{
//Declare a Delegate
delegate int NextProgrammingDelegate(int x, int y, int z);
public class DelegateExample
{
//instance method
public int Add(int a, int b, int c)
{
return (a + b + c);
}
//static method
public static int Average(int a, int b, int c)
{
return (a + b + c) / 3;
}
}
class Program
{
static void Main(string[] args)
{
//instance of delegate
NextProgrammingDelegate objNextProgrammingDelegate;
DelegateExample clsDelegateExample = new DelegateExample();
//reference of instance method to delegate instance
objNextProgrammingDelegate = clsDelegateExample.Add;
//First Way to call delegate
Console.WriteLine("value is " + objNextProgrammingDelegate(3, 3, 2));
//reference of static method to delegate instance
NextProgrammingDelegate objDelegate1 = new NextProgrammingDelegate(DelegateExample.Average);
//Second Way to call delegate
Console.WriteLine("value is " + objDelegate1.Invoke(3, 8, 9));
//Third Way to call delegate
Console.WriteLine("value is " + objDelegate1(3, 4, 5));
Console.ReadLine();
}
}
}
Full Example of Generic Delegate
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NextWithDeleage
{
//Declare a generic delegate
public delegate T Add<T>(T x, T y, T z);
public static class GenericDelegateExample
{
public static int AddInt(int x, int y, int z)
{
return (x + y + z);
}
public static double AddDouble(double x, double y, double z)
{
return (x+y+z);
}
}
class Program
{
static void Main(string[] args)
{
Add<int> objIntTypeDelegate = new Add<int>(GenericDelegateExample.AddInt);
//A new way to reference method to delegate instace
Add<double> objDoubleTypeDelegate = GenericDelegateExample.AddDouble;
Console.WriteLine("Sum of 2, 3 and 4 is " + objIntTypeDelegate(2, 3, 4));
Console.WriteLine("Sum of 2.3, 3.4 and 4.5 is " + objDoubleTypeDelegate(2.3, 3.4, 4.5));
Console.ReadLine();
}
}
}
Conclusion:
Today We learned what is Delegate in C# and what is use of it. We also learned how to create simple delegate and how to create a generic delelegate. Hope you enjoyed.
Reference:
If you want to know more please click here.http://msdn.microsoft.com/en-us/library/ms173171.aspx
I hope this post will help you. Please put your feedback using comment which helps me to improve myself for next post. If you have any doubts please ask your doubts or query in the comment section and If you like this post, please share it with your friends. Thanks.