Delegates

 In C#, delegates are types that represent references to methods. They can be used to define callback methods, event handlers, and can also be used in functional programming patterns. Below is a list of commonly used delegates in C#, including built-in delegates and custom delegates.

Built-in Delegates

  1. Action: Represents a delegate that can point to a method that does not return a value. It can take up to 16 parameters.

    • Examples:
      • Action (no parameters)
      • Action<T> (one parameter)
      • Action<T1, T2> (two parameters)
      • Action<T1, T2, T3> (three parameters)
      • ...
      • Action<T1, T2, ..., T16> (up to 16 parameters)
    csharp
    // Example Action<string> print = message => Console.WriteLine(message);
  2. Func: Represents a delegate that can point to a method that returns a value. It can also take up to 16 parameters.

    • Examples:
      • Func<TResult> (no parameters)
      • Func<T1, TResult> (one parameter)
      • Func<T1, T2, TResult> (two parameters)
      • Func<T1, T2, T3, TResult> (three parameters)
      • ...
      • Func<T1, T2, ..., T16, TResult> (up to 16 parameters)
    csharp
    // Example Func<int, int, int> add = (x, y) => x + y;
  3. Predicate: Represents a delegate that takes one input parameter and returns a boolean value. It is commonly used for filtering data.

    • Example:
      • Predicate<T> (one parameter)
    csharp
    // Example Predicate<int> isEven = number => number % 2 == 0;

Custom Delegates

In addition to the built-in delegates, you can also create custom delegates for specific use cases.

  1. Simple Custom Delegate: A delegate that matches the signature of a method you want to reference.

    csharp
    public delegate void MyCustomDelegate(string message); // Usage MyCustomDelegate myDelegate = message => Console.WriteLine(message);
  2. Delegate with Parameters and Return Value: You can define a delegate that returns a value.

    csharp
    public delegate int MathOperation(int a, int b); // Usage MathOperation operation = (x, y) => x * y; // Example of multiplication

Example Usage of Delegates

Here's a complete example demonstrating the use of built-in and custom delegates:

csharp
using System; public class Program { // Custom delegate declaration public delegate int MathOperation(int a, int b); public static void Main() { // Using built-in Action delegate Action<string> printMessage = message => Console.WriteLine(message); printMessage("Hello, World!"); // Using built-in Func delegate Func<int, int, int> add = (x, y) => x + y; int sum = add(5, 3); Console.WriteLine("Sum: " + sum); // Using custom delegate MathOperation multiply = (x, y) => x * y; int product = multiply(5, 3); Console.WriteLine("Product: " + product); // Using Predicate delegate Predicate<int> isPositive = number => number > 0; Console.WriteLine("Is 5 positive? " + isPositive(5)); Console.WriteLine("Is -1 positive? " + isPositive(-1)); } }


More delegate examples:

public delegate int MathOperation(int x, int y); public static int Add(int x, int y) => x + y; public static int Multiply(int x, int y) => x * y; public static int Divide(int x, int y) => x / y; public static void Main() { Action<string> printMessage = message => Console.WriteLine("Action message: " + message); printMessage("test"); Func<int, int, string> convertSumToString = (x, y) => (x + y).ToString(); Console.WriteLine(convertSumToString(5,7)); Predicate<int> isPositive = number => number > 0; Console.WriteLine(isPositive(5)); var sumOperations = new List<MathOperation>() { Add, Multiply, Divide }; foreach (var operation in sumOperations) { Console.WriteLine(operation(40,5)); } }

Result:

Action message: test
12
True
45
200
8


Summary

  • Built-in Delegates: Action, Func, and Predicate are commonly used in C# for callbacks and functional programming.
  • Custom Delegates: You can create your own delegate types for specific method signatures.
  • Flexibility: Delegates provide a way to reference methods and can be used for events, callbacks, and functional programming styles.

By understanding and utilizing delegates effectively, you can create more modular, reusable, and maintainable code in C#.

Comments

Popular posts from this blog

Microservices and Service-Oriented Architecture

Version control and Continuous Integration/Continuous Deployment (CI/CD)