📜  c# delegate - C# (1)

📅  最后修改于: 2023-12-03 15:13:48.794000             🧑  作者: Mango

C# Delegate

A delegate in C# is a type-safe function pointer that can reference one or more methods with a specific signature. It provides a way to encapsulate and pass around methods as first-class objects. Delegates are frequently used in event handling, callbacks, and implementing the observer pattern.

Declaration

A delegate is declared using the delegate keyword and specifies the return type and parameter types of the methods it can reference. Here is an example declaration of a delegate named MyDelegate:

delegate void MyDelegate(string message);
Instantiation

To instantiate a delegate, you can assign it to a method that matches its signature. You can also combine multiple methods into a single delegate using the + or += operator. Here are some examples:

MyDelegate delegate1 = Method1;
MyDelegate delegate2 = Method2;
MyDelegate delegate3 = delegate1 + delegate2;
MyDelegate delegate4 = null;
delegate4 += delegate3;
Invocation

Once a delegate is instantiated, you can invoke it like a normal method, passing the required arguments. Invoking a delegate will invoke all the methods it references in the order they were added. Here's an example:

delegate3("Hello, World!"); // This will invoke Method1 and Method2
Multicast Delegates

C# delegates can be multicast, which means they can hold references to multiple methods. When a multicast delegate is invoked, all the referenced methods are called. This is useful for scenarios where multiple methods need to be called in response to an event or a callback. Here's an example:

delegate void MyDelegate();

void Method1() { Console.WriteLine("Method1"); }
void Method2() { Console.WriteLine("Method2"); }
void Method3() { Console.WriteLine("Method3"); }

MyDelegate multicastDelegate = Method1 + Method2 + Method3;
multicastDelegate(); // This will invoke all three methods
Delegate Chaining

When a multicast delegate is combined with the - or -= operator, the specified methods are removed from the delegate's invocation list. Chaining delegates allows for dynamic subscription and unsubscription of methods. Here's an example:

multicastDelegate -= Method2;
multicastDelegate(); // This will invoke only Method1 and Method3
Anonymous Methods

C# also supports the use of anonymous methods, which are unnamed inline methods that can be assigned to delegates directly. They provide a convenient way to define simple methods without declaring a separate method. Here's an example:

MyDelegate delegate5 = delegate(string message)
{
    Console.WriteLine($"Anonymous Method: {message}");
};
delegate5("Hello, Delegate!"); // This will invoke the anonymous method
Summary

In summary, C# delegates provide a powerful mechanism for encapsulating and passing around methods as objects. They enable event-driven programming, callbacks, and decoupling of components. Understanding delegates is essential for any C# programmer aiming to write flexible and modular code.