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:
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
For more info http://www.4microsoftsolutions.com/post/Delegates-Multi-Cast-Delegates-in-C.aspx
- 1/23/2012
- Codedefiner4
- 12/14/2011
- cDub Step
"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...
- 2/28/2011
- zign
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
- 2/17/2011
- mcmcalex
- 11/10/2010
- visiontechno
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
- 10/31/2010
- Carlos Adriano Portes
- 8/31/2010
- noisemaker
CS Team: The typo is fixed in later versions of the documentation. Thanks.
- 1/9/2009
- battas
- 4/30/2010
- SJ at MSFT
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"
- 7/2/2009
- michal_czardybon
