Hi!
Today's C# topic, Delegates, is very interesting. Some say, it’s very complex. I hope you have worked on delegate and know about it. However, for freshers, it is a very complex topic. So, I am going to explain it in a simple way. I hope, after that, you will get the basic idea of delegate and love to use delegates in your project.
What is Delegate?
If you have got a chance to work with C++, then you definitely know about function pointer in C++.
Delegate is just like the function pointer in C#, but delegates are type safe. Delegate is an object that points to another method either static or instance. It takes arguments and returns value. It takes reference to other methods. We can invoke or call the method through the delegate object.
Points to remember regarding Delegate
- Using Delegates, we can pass methods and parameters.
- Using Delegates, we can define callback methods.
- We can call multiple methods on a single call using Delegate.
To use Delegate, there are only four steps -
- Declare a delegate type.
- Create or find a method which has same type of delegate.
- Create object/instance of Delegate.
- 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.
-
- delegate int DotnetTutorialDelegate(int x, int y, int z);
-
- 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
-
- DotnetTutorialDelegate objDelegate1 = new DotnetTutorialDelegate(DelegateExample.Average);
-
-
- Add<double> objDoubleTypeDelegate = GenericDelegateExample.AddDouble;
Invoke Delegate object
-
- Console.WriteLine("value is " + objDelegate1.Invoke(3, 8, 9));
-
-
- 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
- {
-
- delegate int DotnetTutorialDelegate(int x, int y, int z);
- public class DelegateExample
- {
-
- public int Add(int a, int b, int c)
- {
- return (a + b + c);
- }
-
- public static int Average(int a, int b, int c)
- {
- return (a + b + c) / 3;
- }
- }
-
- class Program
- {
- static void Main(string[] args)
- {
-
- DotnetTutorialDelegate objDotnetTutorialDelegate;
-
- DelegateExample clsDelegateExample = new DelegateExample();
-
-
- objDotnetTutorialDelegate = clsDelegateExample.Add;
-
-
- Console.WriteLine("value is " + objDotnetTutorialDelegate(3, 3, 2));
-
-
- DotnetTutorialDelegate objDelegate1 = new DotnetTutorialDelegate(DelegateExample.Average);
-
-
- Console.WriteLine("value is " + objDelegate1.Invoke(3, 8, 9));
-
-
- 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
- {
-
- 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);
-
-
- 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 about Delegates in C# and what is the use of it. We also learned how to create simple delegate and how to create a generic delegate. Hope you enjoyed.
Reference
If you want to know more, please click here.
I hope this post helps you. Please put your feedback using comment which will help me improve for the next post. If you have any doubts, please ask your doubts or query in the comment section.