Delegates in C#
Delegates are nothing but it is a reference to the methods and Delegate is a keyword used to create a method object and it will help us to pass the method as parameters.
Declarations
- public delegate TypeOfDelegate Delegate_Name();
Examples
- public delegate string myDelegate(string FirstName, string LastName);
I would like to share the Type of Delegates in C# and please refer the details below,
- Single Delegate
- Multicast Delegate
- Generic Delegate
Sample Programs
- public delegate string myDelegate(string FirstName, string LastName);
- public class Personal {
- public static string myDelegate(string FirstName, string LastName) {
- return FirstName + "" + LastName + "";
- }
- static void Main(string[] args) {
- string FirstName = "Manikandan";
- string LastName = "M";
-
- myDelegate delObj = new myDelegate(myDelegate);
-
- string res = delObj(FirstName, LastName);
- Console.WriteLine("Full Name :" + res);
- Console.ReadLine();
- }
- }
-
Conclusion
This article could help you to understand the delegates in C# using simple Programming.