Delegate (.NET)

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, named SendMessageDelegate, which takes a Message as a parameter and returns void:

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

External links


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Delegate (disambiguation) — Delegate or delegates may refer to: a delegate (politics), a member of a group representing an organisation (such as a union) at a meeting (such as a national conference of unions) delegation, in politics, the act of one assigning responsibility… …   Wikipedia

  • Delegate — Das Wort Delegate bezeichnet in der Informatik einen Delegationsempfänger / Delegierten (OpenStep) einen erweiterten Methodenzeiger in .NET, siehe Delegat (.NET) folgende Städte: Delegate (New South Wales), eine Stadt in Australien …   Deutsch Wikipedia

  • Delegat (.NET) — Dieser Artikel wurde aufgrund von inhaltlichen Mängeln auf der Qualitätssicherungsseite der Redaktion Informatik eingetragen. Dies geschieht, um die Qualität der Artikel aus dem Themengebiet Informatik auf ein akzeptables Niveau zu bringen. Hilf… …   Deutsch Wikipedia

  • Vigile.net — Infobox Website name = Vigile.net url = http://www.vigile.net commercial = No type = Political, press coverage owner = Bernard Frappier author = Bernard Frappier launch date = October 31, 1995 current status = Active|Vigile.net, or Vigile, is an… …   Wikipedia

  • John Penn (delegate) — John Penn (May 17, 1741 ndash September 14, 1788), was a signer of the United States Declaration of Independence as a representative of North Carolina along with Joseph Hewes and William Hooper. Penn was distantly related to William Penn, founder …   Wikipedia

  • C Sharp syntax — The correct title of this article is C# syntax. The substitution or omission of the # sign is because of technical restrictions. Main article: C Sharp (programming language) This article describes the syntax of the C# programming language. The… …   Wikipedia

  • Delegation (programming) — For the authorization related term, see delegation in IT. In object oriented programming, there are two related notions of delegation. Most commonly, it refers to a programming language feature making use of the method lookup rules for… …   Wikipedia

  • Signals and slots — is a language construct introduced in Qt, which makes it easy to implement the Observer pattern while avoiding boilerplate code. The concept is that controls (also known as widgets) can send signals containing event information (e.g. the text… …   Wikipedia

  • Inversion of control — In software engineering, Inversion of Control (IoC) is an abstract principle describing an aspect of some software architecture designs in which the flow of control of a system is inverted in comparison to procedural programming. In traditional… …   Wikipedia

  • Oxygene (programming language) — Oxygene Developer RemObjects Software Stable release 3.0.21 (August 29, 2009; 2 years ago (2009 08 29)) Influenced by Object Pas …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”