154 out of 269 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?
(1500 characters remaining)
Community Content Add
Annotations FAQ
C# Delegates
You can read more about C# Delegates on this URL : http://planetofcoders.com/c-delegates/
Return type is not a part of a method
Sorry, I duplicated...
Return is not part of signature

Plz read it carefully. The signature of a method must beunique in the class in
which the method is declared. The signature of a method consists ofthe name of
the method
,... Therefor your example should be something like this:

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

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

The signature of a method does not include the return type. It is different from the delegate's signature,  which consists of the return type and parameters. And we also can use Covariance for the methods to have a more derived return type than what is defined in the delegate.

hopnguyen

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...
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