134 out of 235 rated this helpful Rate this topic

Delegates (C# Programming Guide)

A delegate is a type that references a method. Once a delegate is assigned a method, it behaves exactly like that method. The delegate method can be used like any other method, with parameters and a return value, as in this example:

public delegate int PerformCalculation(int x, int y);

Any method that matches the delegate's signature, which consists of the return type and parameters, can be assigned to the delegate. This makes is possible to programmatically change method calls, and also plug new code into existing classes. As long as you know the delegate's signature, you can assign your own delegated method.

This ability to refer to a method as a parameter makes delegates ideal for defining callback methods. For example, a sort algorithm could be passed a reference to the method that compares two objects. Separating the comparison code allows the algorithm to be written in a more general way.

Delegates Overview

Delegates have the following properties:

  • Delegates are similar to C++ function pointers, but are type safe.

  • Delegates allow methods to be passed as parameters.

  • Delegates can be used to define callback methods.

  • Delegates can be chained together; for example, multiple methods can be called on a single event.

  • Methods don't need to match the delegate signature exactly. For more information, see Covariance and Contravariance

  • C# version 2.0 introduces the concept of Anonymous Methods, which permit code blocks to be passed as parameters in place of a separately defined method.

In This Section

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 1.11 Delegates

  • 4.2.6 Delegate types

  • 7.5.5.2 Delegate invocations

  • 15 Delegates

See Also

Did you find this helpful?
(2000 characters remaining)
Community Content Add
Annotations FAQ
Thanks for info
thanks for your explanation.

For more info http://www.4microsoftsolutions.com/post/Delegates-Multi-Cast-Delegates-in-C.aspx
@zign
No. it is not possible to pass anything "as a reference." A reference is a value in and of itself, it acts as a "remote control" to another object. We can, however, pass *a* reference *to* a method. The reference would not be the method itself, but a value "pointing" at the method, acting like it.
Grammatical error
I think there is a grammtical error in the second paragraph of the delegates page.
 "For example, a sort algorithm could be passed a reference to the method that compares two objects. Separating the comparison code allows the algorithm to be written in a more general way."
Should it not be passed as a reference?

Just trying to help...
Return is not part of signature

How will the compiler know what to do with

int doCalc(int operand1, int operand2){
//do Calcuation stuff
return intValue
}

vs

string doCalc(int operand1, int operand2){
//do Calculation stuff
//convert int to string representation
return stringValue
}

Therefore, these two methods have the same signature regardless of return type and would not be allowed in the same class
hth

Description of delegates and its usage in dynamic binding of controls with events and Threading
$0A detailed video tutorial has been provided on Delegate's usage for dynamic binding of controls with their events at the below link:$0 $0$0 $0 $0http://visiontechno.net/studymats/delegates.html$0
Return type is not part of a signature (at least in C#)

Return type is not part of a method signature (Can someone confirm please?):

The signature of a method must be unique in the class in which the method is declared. The signature of a method consists of the name of the method, the number of type parameters and the number, modifiers, and types of its parameters. The signature of a method does not include the return type.

C# language specification 4 - Section 1.6.6

A tale of two delegates
delegate does refer to both the class type and the object.  An instance of a delegate class is also called a delegate (oops).  Guess we have to read the code to figure out which we are talking about :(  No one said that C# had to be consistant in the way it named things.  The C# specification V4 refers to 'delegate types' and 'delegate instances'  &15.  As the only place the keyword appears is in the declaration the compiler doesn't have a problem only us poor humans when we try to explain what is going on LOL.
typo
2nd para, 2nd line- read as 'This makes it possible to..'


CS Team: The typo is fixed in later versions of the documentation. Thanks.
Does "delegate" mean a type or an object? It cannot be both...
"A delegate is a type that references a method. Once a delegate is assigned a method, it behaves exactly like that method."

Does then "delegate" mean a type or an object?!

...It cannot be both. It seems to me that you have a single word for two meanings:
(1) a type containing a reference to a method of some specified signature,
(2) an object of that type, which can be actually called like a method.

I would prefer you to be more precise and use "delegate type" for the first case. I have been recently reading a lot about events and delegates and that ambiguity was making me confused many times.

Some other uses of "delegate" word in MSDN in the first meaning:
"Custom event delegates are needed only when an event generates event data"
"A delegate declaration defines a class that is derived from the class System.Delegate"

Some other uses of "delegate" word in MSDN in the second meaning:
"specify a delegate that will be called upon the occurrence of some event"
"Delegates are objects that refer to methods. They are sometimes described as type-safe function pointers"

Advertisement