Compartir a través de


Delegates

The runtime supports reference types called delegates that serve a purpose similar to that of function pointers in C++. Unlike function pointers, a delegate instance is independent of the classes of the methods it encapsulates; all that matters is that those methods be compatible with the delegate's type. Also, while function pointers can only reference static functions, a delegate can reference both static and instance methods. Delegates are mainly used for event handlers and callback functions in the .NET Framework.

All delegates inherit from System.Delegate, and have an invocation list, which is a linked list of methods that are executed when the delegate is invoked. The resulting delegate can reference any method with a matching signature. The return value is not defined for a delegate that has a return type and contains more than one method in its invocation list.

You can use the delegate's Combine and Remove methods to add and remove methods to its invocation list. To call the delegate, use the Invoke method, or the BeginInvoke and EndInvoke methods to call the delegate asynchronously. The implementations of the delegate class are provided by the runtime, not by user code.

See Also

Common Type System | Consuming Events | Type Members | System.Delegate | System.EventHandler