Delegate (.NET)

From Wikipedia, the free encyclopedia

In computer science, delegate is a form of type-safe function pointer used in the .NET Framework technology developed by Microsoft Corporation for computer software development. Delegates specify a method to call and optionally an object to call the method on. They are used to implement callbacks and event listeners.

[edit] Implementation

Although internal implementations may vary, delegate instances can be thought of as a tuples 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.

Delegates are more efficient than interfaces as the method pointer is calculated (if virtual) when the delegate is first assigned, rather than each time it is invoked.

Delegates are a variation of closures.

[edit] References

http://msdn2.microsoft.com/en-us/library/system.delegate.aspx

In other languages