How to use delegates in c#?

Delegates are a type that represents references to methods with a particular parameter list and return type.

Example:


  public delegate int MyDelegate(int x, int y);
  public int Add(int a, int b) 
  {
      return a + b;
  }
  MyDelegate del = Add;
  

Solution:

You can then invoke the delegate:


  int result = del(3, 4);
  Console.WriteLine(result);  // Outputs: 7
  

Beginner's Guide to C#