DEV Community

Emanuel Gustafzon
Emanuel Gustafzon

Posted on

C# Delegates, overview and simple implementation

Are you confused about delegates in C#? No worries, this guide will get you covered!

Overview of delegates in C#.

Delegates are type-safe and secure objects that enable powerful features for methods.

They allow you to pass methods by reference, and because of that, methods can be stored as variables, passed by reference, and chained together.

Delegates are used when working with events, creating callback functions, and adopting the functional programming paradigm in C#.

Delegates make the code concise and readable, especially when dynamically invoking methods. By dynamically invoking methods, I mean methods that are called during runtime with conditions or arguments only known at runtime. User input is a good example of this.

Delegates are highly adopted and used in event driven programming.

implementation

First implement a delegate type. Use the keyword delegate followed by the method signature, the return type and parameters.

public delegate int mathCalculation(int a, int b);
Enter fullscreen mode Exit fullscreen mode

This delegate method is called mathCalculation and obviously is a signature for calculating something.

Any method from any accessible class or struct that matches this signature can be assigned to the delegate.

Create 2 math operation methods with the same signature.

public int addition(int a, int b) {
    return a + b;
}

public int multiply(int a, int b)
{
    return a * b;
}
Enter fullscreen mode Exit fullscreen mode

Assign the methods to the delegate object.

// Use the delegate name to create 2 varibles 
mathCalculation add; 
mathCalculation mult;

// Assign the methods to the varibles. 
add = this.addition; 
mult = this.multiply; 

// Execute
int addResult = add(10, 20);
int multResult = mult(10, 20);

Console.WriteLine($"Addition result: {addResult} multiply result: {multResult}");
Enter fullscreen mode Exit fullscreen mode

Optionally use short hand methods.

Short hand functions cannot be used outside the class but are fast and concise.

add = (int a, int b) => a + b;
// leave out the types.
mult = (a, b) => a * b;

Enter fullscreen mode Exit fullscreen mode

Full example

class Program {

  public delegate int mathCalculation(int a, int b);

  public static int addition(int a, int b) {
      return a + b;
  }

  public static int multiply(int a, int b)
  {
      return a * b;
  }

  public static void Main (string[] args) {
    // Use the delegate name to create 2 varibles 
    mathCalculation add; 
    mathCalculation mult;

    // Assign the methods to the varibles. 
    add = addition; 
    mult = multiply; 

    // Execute
    int addResult = add(10, 20);
    int multResult = mult(10, 20);

    Console.WriteLine($"Addition result: {addResult} multiply result: {multResult}");
  }
}
Enter fullscreen mode Exit fullscreen mode

Short hand version

class Program {

  public delegate int mathCalculation(int a, int b);

  public static void Main (string[] args) {

    mathCalculation add = (a, b) => a + b;
    mathCalculation mult = (a, b) => a * b;

    int addResult = add(10, 20);
    int multResult = mult(10, 20);

    Console.WriteLine($"Addition result: {addResult} multiply result: {multResult}");
  }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)