Junior — Middle
What are the main types of delegates implemented in the .NET platform?
sobes.tech AI
Answer from AI
In the .NET platform, the main types of delegates include:
- Single-cast delegates — represent a reference to a single method.
- Multicast delegates — can contain a list of methods and invoke them sequentially.
Additionally, there are predefined delegates such as Action, Func, and Predicate, which simplify working with methods without the need to create custom delegate types.
Example of a multicast delegate:
public delegate void Notify();
Notify notifyHandlers;
void Handler1() => Console.WriteLine("Handler1 called");
void Handler2() => Console.WriteLine("Handler2 called");
notifyHandlers = Handler1;
notifyHandlers += Handler2;
notifyHandlers(); // Calls Handler1 and Handler2