Delegates are a mechanism of indirectly calling methods at runtime. The primary use of delegates in C# programming is for implementing events and the call-back methods.

Important Points About Delegate

  • Delegate is a strongly typed function pointer (just like pointer is there in C++ language).
  • A Delegate is a reference type variable which holds the reference of a method.
  • We can call delegate like methods in C#.
  • Delegates are used to raise the event in class and associated with the event handler.
  • Delegate can be Non-Generic/ Generic.

Types of Delegate

There are two types of delegate.

Note


Generics are a kind of parameterized data structure that can work with value type as well as reference type. Using generics we can define the data type at runtime using System.Collections.Generic namespace.

  • Single cast Delegate

    1. Single cast Delegate: A single cast delegate is derived from System.Delagate Class.
    2. It contains reference of only one method at a time.

  • Multi-cast Delegate

    1. Multi-cast Delegate: Multicast Delegate is derived from System.MulticatsDelagate class.
    2. It Creates an invocation list of multiple methods.
    3. In Multicast delegate we create a single delegate that invokes multiple methods.
    4. It holds the reference of multiple methods and it executes all the methods as in calling order.
    5. The multiple method called by delegate should not return any values.

Syntax

< public delegate return Type(void/string) delegate_Name (); >

Or

< public delegate return Type(void/string) delegate_Name <T> (); >

Example

Single Cast Non-Generic/Generic Delegate,

  1. namespace Demo_Delegate
  2. {
  3. class Single_Cast
  4. {
  5. //Declare Non-Generic Single cast delegate
  6. public delegate void Single_Cast_delegate();
  7. //Private Method to be called
  8. private static void message()
  9. {
  10. Console.WriteLine("This Example of Single Cast Non-generic Delegate");
  11. }
  12. static void Main(string[] args)
  13. {
  14. //Instance created for Single Cast Delagate
  15. Single_Cast_delegate SCD = new Single_Cast_delegate(message);
  16. //Instance created for Single Cast Generic Delagate
  17. Single_Cast_Generic SCG = new Single_Cast_Generic();
  18. Mydel<int> my1 = new Mydel<int>(SCG.add);
  19. Mydel<string> my2 = new Mydel<string>(SCG.Myname);
  20. //Calling of Delegate
  21. SCD();
  22. my1(10, 20);
  23. my2("My", "Name");
  24. }
  25. }
  26. //Declare Generic Single cast delegate
  27. public delegate void Mydel<T>(T a, T b);
  28. class Single_Cast_Generic
  29. {
  30. public void add(int a, int b)
  31. {
  32. int Sum = a + b;
  33. Console.WriteLine(Sum);
  34. }
  35. public void Myname(string firstname, string lastname)
  36. {
  37. string fn = firstname + " " + lastname;
  38. Console.WriteLine(fn);
  39. }
  40. }
  41. }
Multicast Non-Generic/Generic Delegate,
  1. namespace Demo_Delegate
  2. {
  3. class MyClass
  4. {
  5. static void Main(string[] args)
  6. {
  7. //Instance created for Muticaste Cast Delagate
  8. MultiCast_delegate md = new MultiCast_delegate();
  9. NonGenericMulticaste NGM = new NonGenericMulticaste(md.FirstMessage);
  10. // Add another method to the invocation list
  11. NGM += new NonGenericMulticaste(md.SecondMessage);
  12. NGM("Call Mutliple methods using single Object");
  13. //Instance created for MultiCast Generic Delagate
  14. MultiCast_Genericdelegate MG = new MultiCast_Genericdelegate();
  15. //We can call both method having diffrent type with single delegate
  16. GenericMulticaste<int> obj1 = new GenericMulticaste<int>(MG.FirstMessage);
  17. obj1 += new GenericMulticaste<int>(MG.Message);
  18. GenericMulticaste<string> obj2 = new GenericMulticaste<string>(MG.SecondMessage);
  19. obj2 += new GenericMulticaste<string>(MG.SecondMessage);
  20. obj1(100);
  21. obj2("Call Mutliple Generic methods using single Object");
  22. }
  23. }
  24. //Create a multicate Delegate
  25. public delegate void NonGenericMulticaste(string Message);
  26. class MultiCast_delegate
  27. {
  28. public void FirstMessage(string Message1)
  29. {
  30. Console.WriteLine(Message1);
  31. }
  32. public void SecondMessage(string Message2)
  33. {
  34. Console.WriteLine(Message2);
  35. }
  36. }
  37. //Create a multicate Generic Delegate
  38. public delegate void GenericMulticaste<T>(T Message);
  39. class MultiCast_Genericdelegate
  40. {
  41. public void FirstMessage(int Number)
  42. {
  43. Console.WriteLine("Your Number is:"+Number);
  44. }
  45. public void Message(int Number)
  46. {
  47. Console.WriteLine("Your Number is:" + Number);
  48. }
  49. public void SecondMessage(string Generic_Message)
  50. {
  51. Console.WriteLine(Generic_Message);
  52. }
  53. public void Second(string Generic_Message)
  54. {
  55. Console.WriteLine(Generic_Message);
  56. }
  57. }
  58. }
Following are the predefined generic delegates,
  1. public delegate TResult Func<in T, out TResult>(T arg);
  2. public delegate void Action()
  3. public delegate bool Predicate<in T>(T obj);
Example
  1. namespace PreDefinedGenericDelegates
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. //Action Delegate
  8. Action<int,int> a = new Action<int,int>(MyClass.sum);
  9. a(5, 6);
  10. Action<string, string> b = new Action<string, string>(MyClass.add);
  11. b("Kundan", "Jha");
  12. //Func Delegate
  13. Func<int, int, int> ds = new Func<int, int, int>(MyClass.Multi);
  14. Console.WriteLine(ds(45, 65));
  15. //Predicate Delegate
  16. Predicate<string> p = new Predicate<string>(MyClass.IsDate);
  17. if (p("01-01-2015"))
  18. {
  19. Console.WriteLine("Valid");
  20. }
  21. else
  22. {
  23. Console.WriteLine("Invalid");
  24. }
  25. Console.ReadKey();
  26. }
  27. }
  28. class MyClass
  29. {
  30. public static bool IsDate(string date)
  31. {
  32. DateTime dt;
  33. return DateTime.TryParse(date, out dt);
  34. }
  35. public static int Multi(int a, int b)
  36. {
  37. return a + b;
  38. }
  39. public static void sum(int num1, int num2)
  40. {
  41. int sum = num1 + num2;
  42. Console.WriteLine(sum);
  43. }
  44. public static void add(string fname, string lname)
  45. {
  46. string fullname = fname + " " + lname;
  47. Console.WriteLine(fullname);
  48. }
  49. }
  50. }
I hope you liked this article. Thank You.