How do you think about delegate in c#?
-
I read a book about c# called. I find delegate is harder to master than c++'s pointer... I dont know why everybody say that c# is easier to use than c++. sigh...
-
I read a book about c# called. I find delegate is harder to master than c++'s pointer... I dont know why everybody say that c# is easier to use than c++. sigh...
Actually,if c# has pointer instead of delegate. It will be perfect
-
I read a book about c# called. I find delegate is harder to master than c++'s pointer... I dont know why everybody say that c# is easier to use than c++. sigh...
What don't you get about delegates in particular? - Nick Parker
My Blog -
I read a book about c# called. I find delegate is harder to master than c++'s pointer... I dont know why everybody say that c# is easier to use than c++. sigh...
A delegate is a managed function pointer, and even offers several advantages. First, when compiled (for compilers that support this, as the C# compiler does) the compiler automatically generates asynchronous invocation methods! You can't beat that! :) Take the following declarations in C:
BOOL CALLBACK EnumProc(LPCTSTR s);
BOOL EnumSomething(EnumProc proc);In C#, this would look like:
public delegate bool EnumProc(string s);
// In some class...
public bool EnumSomething(EnumProc proc);When you call
EnumSomething
, you would do so in a similar manner as with C:bool val = EnumSomething(new EnumProc(MyEnumProc));
//...
private bool MyEnumProc(string s)
{
Console.WriteLine(s);
}In the implementation for
EnumSomething
, you could even useBeginInvoke
instead ofInvoke
(or the shorthand way where you just use the delegate parameter like a function) for asynchronous invocation. See Handling and Raising Events[^] and Including Asynchronous Calls[^] in MSDN for more information, in-depth topics, and plenty of examples. Also, just so you can see there really isn't much different, when interop'ing methods that require a function pointer or P/Invoke functions that require function pointers, a delegate is what you use in lieu of anIntPtr
. When the param is marshaled, it is marshaled as a function pointer.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
A delegate is a managed function pointer, and even offers several advantages. First, when compiled (for compilers that support this, as the C# compiler does) the compiler automatically generates asynchronous invocation methods! You can't beat that! :) Take the following declarations in C:
BOOL CALLBACK EnumProc(LPCTSTR s);
BOOL EnumSomething(EnumProc proc);In C#, this would look like:
public delegate bool EnumProc(string s);
// In some class...
public bool EnumSomething(EnumProc proc);When you call
EnumSomething
, you would do so in a similar manner as with C:bool val = EnumSomething(new EnumProc(MyEnumProc));
//...
private bool MyEnumProc(string s)
{
Console.WriteLine(s);
}In the implementation for
EnumSomething
, you could even useBeginInvoke
instead ofInvoke
(or the shorthand way where you just use the delegate parameter like a function) for asynchronous invocation. See Handling and Raising Events[^] and Including Asynchronous Calls[^] in MSDN for more information, in-depth topics, and plenty of examples. Also, just so you can see there really isn't much different, when interop'ing methods that require a function pointer or P/Invoke functions that require function pointers, a delegate is what you use in lieu of anIntPtr
. When the param is marshaled, it is marshaled as a function pointer.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Wow! Thanks Heath. I seem to learn something new each time I read one of your posts. I never knew that you could do Asynchronous calls on delegates. --Colin Mackay--
-
Wow! Thanks Heath. I seem to learn something new each time I read one of your posts. I never knew that you could do Asynchronous calls on delegates. --Colin Mackay--
Yep, just a word of warning, though - if the delegate is declared in the current project, the
BeginInvoke
andEndInvoke
methods don't show up in IntelliSense but they're there! :) If you read the second article link, you can see the generated signatures of those methods; or, you could always compile your project once and examine the method signatures with ildasm.exe.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----