Delegate (CLI)

A delegate is a form of type-safe function pointer used by the Common Language Infrastructure (CLI). Delegates specify a method to call and optionally an object to call the method on. Delegates are used, among other things, to implement callbacks and event listeners. A delegate object encapsulates a reference to a method. The delegate object can then be passed to code that can call the referenced method, without having to know at compile time which method will be invoked.

A multicast delegate is a delegate that points to several methods.[1][2] Multicast delegation is a mechanism that provides functionality to execute more than one method. There is a list of delegates maintained internally, and when the multicast delegate is invoked, the list of delegates is executed.

In C#, delegates are often used to implement callbacks in event driven programming. For example, a delegate may be used to indicate which method should be called when the user clicks on some button. Delegates allow the programmer to notify several methods that an event has occurred.[3]

  1. ^ Microsoft Developer Network (MSDN) Article, How to: Combine Delegates (Multicast Delegates)(C# Programming Guide), Accessed 5/20/2008
  2. ^ "About Microsoft's "Delegates"". Sun Developer Network. Sun Microsystems. Archived from the original on 10 February 1999.
  3. ^ Wikibooks:C Sharp Programming/Delegates and Events