Sobes.tech
Junior — Middle

Tell about the different types of delegates you know.

sobes.tech AI

Answer from AI

In C#, delegates are types that represent references to methods. The main types of delegates:

  • Regular delegates — explicitly defined, can reference methods with a specific signature.

  • Multicast delegates — can contain a list of methods called sequentially.

  • Action — a built-in delegate with no return value, with parameters from 0 to 16.

  • Func — a built-in delegate with a return value, with parameters and result type specified.

  • Predicate — a delegate that takes one parameter and returns a bool.

Example of a regular delegate:

public delegate void Notify(string message);

Notify notify = message => Console.WriteLine(message);
notify("Hello");

A multicast delegate allows subscribing multiple methods:

notify += AnotherMethod;
Tell about the different types of delegates you know… - sobes.tech