- Delegate (.NET)
-
A delegate is a form of type-safe function pointer used by the .NET Framework. Delegates specify a method to call and optionally an object to call the method on. They are used, among other things, to implement callbacks and event listeners. It encapsulates a reference of a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.
Contents
C# Code Example ==
Code to declare a
delegate
type, namedSendMessageDelegate
, which takes aMessage
as a parameter and returnsvoid
:delegate void SendMessageDelegate(Message message);
Code to define a method which takes an instantiated delegate as its argument:
void SendMessage(SendMessageDelegate sendMessageDelegateReference) { // call the delegate and any other chained delegates synchronously sendMessageDelegateReference(new Message("hello this is a sample message")); }
The implemented method which runs when the delegate is called:
void HandleSendMessage(Message message) { // the implementation for the Sender and Message classes are not relevant to this example Sender.Send(message); }
Code to call the SendMessage method, passing an instantiated delegate as an argument:
SendMessage(new SendMessageDelegate(HandleSendMessage));
Technical Implementation Details
Although internal implementations may vary, delegate instances can be thought of as a tuple of an object and a method pointer and a reference (possibly null) to another delegate. Hence a reference to one delegate is possibly a reference to multiple delegates. When the first delegate has finished, if its chain reference is not null, the next will be invoked, and so on until the list is complete. This pattern allows an event to have overhead scaling easily from that of a single reference up to dispatch to a list of delegates, and is widely used in the .NET Framework.
Performance
Performance of delegates used to be much slower than a virtual or interface method call (6 to 8 times slower in Microsoft's 2003 benchmarks),[1] but, in .NET 2005, it is about the same as interface calls.[2] This means there is a small added overhead compared to direct method invocations.
There are very stringent rules on the construction of delegate classes. These rules permit optimizing compilers a great deal of leeway when optimizing delegates while ensuring type safety.[citation needed]
Delegates are a variation of closures.
See also
References
- ^ Gray, Jan (June 2003). "Writing Faster Managed Code: Know What Things Cost". Microsoft. http://msdn2.microsoft.com/en-us/library/ms973852. Retrieved 2007-09-09.
- ^ Sturm, Oliver (2005-09-01). "Delegate calls vastly sped up in .NET 2". http://www.sturmnet.org/blog/archives/2005/09/01/. Retrieved 2007-09-09.
External links
- MSDN documentation for Delegates
- Sun's critique of Delegates
- Microsoft answer to Sun
- Inner workings of Delegates
- PerfectJPattern Open Source Project, Provides componentized i.e. context-free and type-safe implementation of the Delegates Pattern in Java
Categories:- .NET framework
- Subroutines
Wikimedia Foundation. 2010.