Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
AnswerRe: Query on Delegate Pin
OriginalGriff11-Jul-12 21:37
mveOriginalGriff11-Jul-12 21:37 
GeneralRe: Query on Delegate Pin
Simon_Whale11-Jul-12 22:27
Simon_Whale11-Jul-12 22:27 
GeneralRe: Query on Delegate Pin
ashish711-Jul-12 22:29
ashish711-Jul-12 22:29 
GeneralRe: Query on Delegate Pin
DaveyM6911-Jul-12 22:48
professionalDaveyM6911-Jul-12 22:48 
GeneralRe: Query on Delegate Pin
OriginalGriff11-Jul-12 22:55
mveOriginalGriff11-Jul-12 22:55 
GeneralRe: Query on Delegate Pin
ashish711-Jul-12 23:13
ashish711-Jul-12 23:13 
GeneralRe: Query on Delegate Pin
Pete O'Hanlon11-Jul-12 23:23
mvePete O'Hanlon11-Jul-12 23:23 
GeneralRe: Query on Delegate Pin
BobJanova11-Jul-12 23:41
BobJanova11-Jul-12 23:41 
In .Net 1, you had to explicitly instantiate a delegate from a method:

EventHandler GameStarted = new EventHandler(GameStartedMethod);

void GameStartedMethod(object sender, EventArgs e) { ... }


Although clear, this was a bit messy and wasted a lot of time, so in .Net 2 and up you are allowed to implicitly cast the method, even though that appears to be assigning a method to a variable/property:
EventHandler GameStarted = GameStartedMethod;

void GameStartedMethod(object sender, EventArgs e) { ... }


You are also allowed to create an anonymous, inline method to assign to the delegate, using the delegate keyword:

EventHandler GameStarted = delegate(object sender, EventArgs e) { ... }


The delegate keyword approach allows you to omit the parameter list, if you don't need to refer to the parameters, which you often don't with event handlers:

EventHandler GameStarted = delegate { ... }


Finally, in .Net 3 and up, you can use lambda functions to create anonymous functions that can be used as a delegate:

EventHandler GameStarted = (sender, e) => { ... }


All of these are equivalent, and result in a delegate that points at a method. In the first two cases, that method has a name and can be called normally as well; in the others, it is anonymous and only exists in the context of the delegate.

When calling a delegate, normal method syntax is used.
GeneralRe: Query on Delegate Pin
Pete O'Hanlon11-Jul-12 22:50
mvePete O'Hanlon11-Jul-12 22:50 
GeneralRe: Query on Delegate Pin
OriginalGriff11-Jul-12 22:56
mveOriginalGriff11-Jul-12 22:56 
GeneralRe: Query on Delegate Pin
DaveyM6911-Jul-12 22:57
professionalDaveyM6911-Jul-12 22:57 
GeneralRe: Query on Delegate Pin
Wayne Gaylard11-Jul-12 23:02
professionalWayne Gaylard11-Jul-12 23:02 
GeneralRe: Query on Delegate Pin
ashish712-Jul-12 2:15
ashish712-Jul-12 2:15 
GeneralRe: Query on Delegate Pin
OriginalGriff12-Jul-12 2:23
mveOriginalGriff12-Jul-12 2:23 
GeneralRe: Query on Delegate Pin
ashish712-Jul-12 2:32
ashish712-Jul-12 2:32 
GeneralRe: Query on Delegate Pin
ashish712-Jul-12 2:44
ashish712-Jul-12 2:44 
QuestionRe: Query on Delegate Pin
DaveyM6912-Jul-12 3:22
professionalDaveyM6912-Jul-12 3:22 
AnswerRe: Query on Delegate Pin
ashish712-Jul-12 3:31
ashish712-Jul-12 3:31 
GeneralRe: Query on Delegate Pin
DaveyM6912-Jul-12 7:38
professionalDaveyM6912-Jul-12 7:38 
GeneralRe: Query on Delegate Pin
ashish712-Jul-12 7:53
ashish712-Jul-12 7:53 
GeneralRe: Query on Delegate Pin
OriginalGriff12-Jul-12 3:48
mveOriginalGriff12-Jul-12 3:48 
GeneralRe: Query on Delegate Pin
ashish712-Jul-12 3:58
ashish712-Jul-12 3:58 
GeneralRe: Query on Delegate Pin
OriginalGriff12-Jul-12 4:11
mveOriginalGriff12-Jul-12 4:11 
GeneralRe: Query on Delegate Pin
DaveyM6912-Jul-12 4:32
professionalDaveyM6912-Jul-12 4:32 
AnswerRe: Query on Delegate Pin
Abhinav S11-Jul-12 21:53
Abhinav S11-Jul-12 21:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.