Delegates

Delegates in C# are type-safe function pointers that allow methods to be treated as objects, making it possible to pass them as parameters. They enable the implementation of callback functions and event handling by encapsulating a method with a specific signature. Delegates are particularly useful for designing extensible and flexible applications, as they allow methods to be assigned and invoked dynamically.

Basics of Delegates

  • Declaration: A delegate is declared using the ‘delegate’ keyword, followed by a method signature.
  • Instantiation: To use a delegate, you must create an instance of it and pass in a method that matches its signature.
  • Invocation: A delegate can be invoked just like a regular method call.

Example

using System;

// Declare a delegate
delegate int Operation(int a, int b);
class Program
{
  // Method for addition
  static int Add(int x, int y)
  {
      return x + y;
  }

    // Method for subtraction
  static int Subtract(int x, int y)
  {
      return x - y;
  }

    static void Main()
  {
      // Instantiate the delegate with the Add method
      Operation op = new Operation(Add);

        // Invoke the delegate
      Console.WriteLine($"Addition: {op(20, 10)}");

        // Change the delegate to reference the Subtract method
      op = new Operation(Subtract);

        // Invoke the delegate
      Console.WriteLine($"Subtraction: {op(20, 10)}");

        // Using anonymous methods with delegates
      op = delegate (int a, int b) {
          return a * b;
      };
      Console.WriteLine($"Multiplication: {op(20, 10)}");

        // Using lambda expressions with delegates
      op = (a, b) => a / b;
      Console.WriteLine($"Division: {op(20, 10)}");
  }
}

Explanation of the Example

  1. Delegate Declaration:
    • We declare a delegate named ‘Operation’ that takes two integer parameters and returns an integer using the ‘delegate’ keyword.
  2. Addition and Subtraction Methods:
    • We define two methods, ‘Add’ and ‘Subtract’, which match the signature of the delegate.
  3. Main Method:
    • In the main method, we instantiate the ‘Operation’ delegate with the ‘Add’ method and invoke it to perform addition.
    • Next, we change the delegate to reference the ‘Subtract’ method and invoke it to perform subtraction.
    • Additionally, we demonstrate the use of anonymous methods and lambda expressions with delegates to perform multiplication and division, respectively.

Benefits of Using Delegates

  • Flexibility: Delegates allow methods to be passed as parameters, providing flexibility in designing extensible and dynamic applications.
  • Event Handling: Delegates are fundamental to event handling in C#. They enable events to be raised and handled dynamically.
  • Callbacks: Delegates facilitate callbacks, allowing one method to specify another method to be executed when an operation completes or an event occurs.
  • Encapsulation: Delegates encapsulate method calls, allowing methods to be assigned, stored, and invoked dynamically.
Post a comment

Leave a Comment

Scroll to Top