All Topics  
Delegate (.NET)

 

   Email Print
   Bookmark   Link






 

Delegate (.NET)



 
 
A delegate is a form of type-safe
Type safety

In computer science, type safety is a property of some programming languages that is defined differently by different communities, but most definitions involve the use of a type system to prevent certain erroneous or undesirable program behavior ....
 function pointer
Function pointer

A function pointer is a type of pointer in C , C++, D programming language, and other C-like programming languages. When Dereference operator, a function pointer invokes a subroutine, passing it zero or more arguments just like a normal function....
 used by the .NET Framework
.NET Framework

The Microsoft .NET Framework is a software framework that is available with several Microsoft Windows operating systems. It includes a large Library of coded solutions to prevent common programming problems and a virtual machine that manages the execution of programs written specifically for the Software framework....
. Delegates specify a method
Method (computer science)

In object-oriented programming, a method is a subroutine that is exclusively associated either with a class or with an object . Like a procedure in procedural programming languages, a method usually consists of a sequence of statement to perform an action, a set of input parameter to customize those actions, and possibly an output value...
 to call and optionally an object
Object (computer science)

In its simplest embodiment, an object is an allocated region of storage. Since programming languages use variable#Computer_programmings to access objects, the terms object and variable are often used interchangeably....
 to call the method on. They are used, among other things, to implement callbacks
Callback (computer science)

In computer programming, a callback is executable code that is passed as an argument to other code. It allows a lower-level abstraction layer to call a subroutine defined in a higher-level layer....
 and event listeners.

ough internal implementation
Implementation

Implementation is the realization of an application, or execution of a plan, idea, model, design, specification, Standardization, algorithm, or policy....
s may vary, delegate instances
Object (computer science)

In its simplest embodiment, an object is an allocated region of storage. Since programming languages use variable#Computer_programmings to access objects, the terms object and variable are often used interchangeably....
 can be thought of as a tuple
Tuple

In mathematics, a tuple is a sequence of a specific number of values, called the components of the tuple. These components can be any kind of mathematical objects, where each component of a tuple is a value of a specified type....
 of an object
Object (computer science)

In its simplest embodiment, an object is an allocated region of storage. Since programming languages use variable#Computer_programmings to access objects, the terms object and variable are often used interchangeably....
 and a method
Method (computer science)

In object-oriented programming, a method is a subroutine that is exclusively associated either with a class or with an object . Like a procedure in procedural programming languages, a method usually consists of a sequence of statement to perform an action, a set of input parameter to customize those actions, and possibly an output value...
 pointer and a reference
Reference (computer science)

In computer science, a reference is an object containing information about how to locate and access the particular data item, as opposed to containing the data itself....
 (possibly null) to another delegate.






Discussion
Ask a question about 'Delegate (.NET)'
Start a new discussion about 'Delegate (.NET)'
Answer questions from other users
Full Discussion Forum



Encyclopedia


A delegate is a form of type-safe
Type safety

In computer science, type safety is a property of some programming languages that is defined differently by different communities, but most definitions involve the use of a type system to prevent certain erroneous or undesirable program behavior ....
 function pointer
Function pointer

A function pointer is a type of pointer in C , C++, D programming language, and other C-like programming languages. When Dereference operator, a function pointer invokes a subroutine, passing it zero or more arguments just like a normal function....
 used by the .NET Framework
.NET Framework

The Microsoft .NET Framework is a software framework that is available with several Microsoft Windows operating systems. It includes a large Library of coded solutions to prevent common programming problems and a virtual machine that manages the execution of programs written specifically for the Software framework....
. Delegates specify a method
Method (computer science)

In object-oriented programming, a method is a subroutine that is exclusively associated either with a class or with an object . Like a procedure in procedural programming languages, a method usually consists of a sequence of statement to perform an action, a set of input parameter to customize those actions, and possibly an output value...
 to call and optionally an object
Object (computer science)

In its simplest embodiment, an object is an allocated region of storage. Since programming languages use variable#Computer_programmings to access objects, the terms object and variable are often used interchangeably....
 to call the method on. They are used, among other things, to implement callbacks
Callback (computer science)

In computer programming, a callback is executable code that is passed as an argument to other code. It allows a lower-level abstraction layer to call a subroutine defined in a higher-level layer....
 and event listeners.

Implementation

Although internal implementation
Implementation

Implementation is the realization of an application, or execution of a plan, idea, model, design, specification, Standardization, algorithm, or policy....
s may vary, delegate instances
Object (computer science)

In its simplest embodiment, an object is an allocated region of storage. Since programming languages use variable#Computer_programmings to access objects, the terms object and variable are often used interchangeably....
 can be thought of as a tuple
Tuple

In mathematics, a tuple is a sequence of a specific number of values, called the components of the tuple. These components can be any kind of mathematical objects, where each component of a tuple is a value of a specified type....
 of an object
Object (computer science)

In its simplest embodiment, an object is an allocated region of storage. Since programming languages use variable#Computer_programmings to access objects, the terms object and variable are often used interchangeably....
 and a method
Method (computer science)

In object-oriented programming, a method is a subroutine that is exclusively associated either with a class or with an object . Like a procedure in procedural programming languages, a method usually consists of a sequence of statement to perform an action, a set of input parameter to customize those actions, and possibly an output value...
 pointer and a reference
Reference (computer science)

In computer science, a reference is an object containing information about how to locate and access the particular data item, as opposed to containing the data itself....
 (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 Program flow is determined by event s — i.e., sensor outputs or user actions or Message passing from other programs or Thread_....
 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 Microsoft .NET Framework is a software framework that is available with several Microsoft Windows operating systems. It includes a large Library of coded solutions to prevent common programming problems and a virtual machine that manages the execution of programs written specifically for the Software framework....
.

Performance of delegates used to be much slower than a virtual
Virtual function

In object-oriented programming, a virtual function or virtual method is one whose behavior can be Method overriding within an inheriting class by a function with the same Method signature....
 or interface
Interface (computer science)

Interface generally refers to an Abstraction_%28computer_science%29 that an entity provides of itself to the outside. This separates the methods of external communication from internal operation, and allows it to be internally modified without affecting the way outside entities interact with it, as well as provide Polymorphism in object-orien...
 method call (6 to 8 times slower in Microsoft's 2003 benchmarks), but it is now about the same as interface calls. This means there is a small added overhead compared to direct method invocations.

Delegates are a variation of closures
Closure (computer science)

In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables....
.

See also


  • Delegate method
  • Delegation (programming)
    Delegation (programming)

    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 dispatching so-called self-calls as defined by Lieberman in his 1986 paper "Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems"....


External links

  • , Provides componentized i.e. context-free and type-safe implementation of the Delegates Pattern in Java