Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do you think about delegate in c#?

How do you think about delegate in c#?

Scheduled Pinned Locked Moved C#
csharpc++questionlearning
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Forrest Feather
    wrote on last edited by
    #1

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

    F N H 3 Replies Last reply
    0
    • F Forrest Feather

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

      F Offline
      F Offline
      Forrest Feather
      wrote on last edited by
      #2

      Actually,if c# has pointer instead of delegate. It will be perfect

      1 Reply Last reply
      0
      • F Forrest Feather

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

        N Offline
        N Offline
        Nick Parker
        wrote on last edited by
        #3

        What don't you get about delegates in particular? - Nick Parker
          My Blog

        1 Reply Last reply
        0
        • F Forrest Feather

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

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          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 use BeginInvoke instead of Invoke (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 an IntPtr. 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-----

          C 1 Reply Last reply
          0
          • H Heath Stewart

            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 use BeginInvoke instead of Invoke (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 an IntPtr. 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-----

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

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

            EuroCPian Spring 2004 Get Together[^]

            H 1 Reply Last reply
            0
            • C Colin Angus 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--

              EuroCPian Spring 2004 Get Together[^]

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              Yep, just a word of warning, though - if the delegate is declared in the current project, the BeginInvoke and EndInvoke 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-----

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups