Sobes.tech
Junior

What delegates do you know?.

sobes.tech AI

Answer from AI

Main types of delegates in .NET:

  • System.Delegate (base abstract class) — the foundation for all delegates.
  • System.MulticastDelegate (inherits from Delegate) — allows a delegate to contain multiple methods.
  • Generic delegates in .NET Framework 3.5+:
    • Action — a delegate with no return value (void).
    • Action<T> ... Action<T1, T2, T3, T4> — delegates with one to four parameters and no return value.
    • Func<TResult> — a delegate with a return value of type TResult and no parameters.
    • Func<T, TResult> ... Func<T1, T2, T3, T4, TResult> — delegates with one to four parameters and a return value of type TResult.
    • Predicate<T> — a special type of Func<T, bool>, representing a method that defines a set of criteria and determines whether the specified object meets those criteria.

Example of using Action and Func:

// Action delegate: takes an int, returns nothing
Action<int> printNumber = (num) => Console.WriteLine(num);
printNumber(10); // Outputs 10

// Func delegate: takes two ints, returns an int
Func<int, int, int> addNumbers = (a, b) => a + b;
int result = addNumbers(5, 3); // result = 8

Delegates are also used in events (event).

What delegates do you know?. — C# - sobes.tech