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
  1. public delegate TypeOfDelegate Delegate_Name();
Examples
  1. 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
  1. public delegate string myDelegate(string FirstName, string LastName);
  2. public class Personal {
  3. public static string myDelegate(string FirstName, string LastName) {
  4. return FirstName + "" + LastName + "";
  5. }
  6. static void Main(string[] args) {
  7. string FirstName = "Manikandan";
  8. string LastName = "M";
  9. //Creating the Delegate object
  10. myDelegate delObj = new myDelegate(myDelegate);
  11. //use a delegate for processing
  12. string res = delObj(FirstName, LastName);
  13. Console.WriteLine("Full Name :" + res);
  14. Console.ReadLine();
  15. }
  16. }
  17. //Output: Manikandan M

Conclusion

This article could help you to understand the delegates in C# using simple Programming.