Delegate (.NET)
Encyclopedia
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.
C# Code Example
Code to declare a
Code to define a method which takes an instantiated delegate as its argument:
The implemented method which runs when the delegate is called:
Code to call the SendMessage method, passing an instantiated delegate as an argument:
Technical Implementation Details
Although internal implementation
s 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), but, in .NET 2005, it is about the same as interface calls. 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.
Delegates are a variation of closures
.
External links
Type safety
In computer science, type safety is the extent to which a programming language discourages or prevents type errors. A type error is erroneous or undesirable program behaviour caused by a discrepancy between differing data types...
function pointer
Function pointer
A function pointer is a type of pointer in C, C++, D, and other C-like programming languages, and Fortran 2003. When dereferenced, a function pointer can be used to invoke a function and pass it arguments just like a normal function...
used by the .NET Framework
.NET Framework
The .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...
. Delegates specify a method
Method (computer science)
In object-oriented programming, a method is a subroutine associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time...
to call and optionally an object
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...
to call the method on. They are used, among other things, to implement callbacks
Callback (computer science)
In computer programming, a callback is a reference to executable code, or a piece of executable code, that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine defined in a higher-level layer....
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.
C# Code Example
Code to declare a
delegate
type, named SendMessageDelegate
, which takes a Message
as a parameter and returns void
:Code to define a method which takes an instantiated delegate as its argument:
The implemented method which runs when the delegate is called:
Code to call the SendMessage method, passing an instantiated delegate as an argument:
Technical Implementation Details
Although internal implementation
Implementation
Implementation is the realization of an application, or execution of a plan, idea, model, design, specification, standard, algorithm, or policy.-Computer Science:...
s may vary, delegate instances
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...
can be thought of as a tuple
Tuple
In mathematics and computer science, a tuple is an ordered list of elements. In set theory, an n-tuple is a sequence of n elements, where n is a positive integer. There is also one 0-tuple, an empty sequence. An n-tuple is defined inductively using the construction of an ordered pair...
of an object
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...
and a method
Method (computer science)
In object-oriented programming, a method is a subroutine associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time...
pointer and a reference
Reference (computer science)
In computer science, a reference is a value that enables a program to indirectly access a particular data item, such as a variable or a record, in the computer's memory or in some other storage device. The reference is said to refer to the data item, and accessing those data is called...
(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
Event-driven programming
In computer programming, event-driven programming or event-based programming is a programming paradigm in which the flow of the program is determined by events—i.e., sensor outputs or user actions or messages from other programs or threads.Event-driven programming can also be defined as an...
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
.NET Framework
The .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...
.
Performance
Performance of delegates used to be much slower than a virtual
Virtual function
In object-oriented programming, a virtual function or virtual method is a function or method whose behaviour can be overridden within an inheriting class by a function with the same signature...
or interface
Interface (computer science)
In the field of computer science, an interface is a tool and concept that refers to a point of interaction between components, and is applicable at the level of both hardware and software...
method call (6 to 8 times slower in Microsoft's 2003 benchmarks), but, in .NET 2005, it is about the same as interface calls. 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.
Delegates are a variation of closures
Closure (computer science)
In computer science, a closure is a function together with a referencing environment for the non-local variables of that function. A closure allows a function to access variables outside its typical scope. Such a function is said to be "closed over" its free variables...
.
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