Delegates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stephen

    Delegates

    I am new to C# and can't get my head round what delegates
    are and what they are for. can anyone enlighten me?
  • Michael Bird

    #2
    Re: Delegates

    Delegates, at the simplest explanation, are function pointers. You use them
    to do callbacks.


    "Stephen" <anonymous@disc ussions.microso ft.com> wrote in message
    news:0bf301c3a4 7b$617d7350$a60 1280a@phx.gbl.. .[color=blue]
    > I am new to C# and can't get my head round what delegates
    > are and what they are for. can anyone enlighten me?[/color]


    Comment

    • Tu-Thach

      #3
      Delegates

      If you are from C/C++, they are similar to function
      pointers. It is used for callbacks, etc.

      Tu-Thach
      [color=blue]
      >-----Original Message-----
      >I am new to C# and can't get my head round what delegates
      >are and what they are for. can anyone enlighten me?
      >.
      >[/color]

      Comment

      • Daniel Pratt

        #4
        Re: Delegates

        Hi Stephen,

        "Stephen" <anonymous@disc ussions.microso ft.com> wrote in message
        news:0bf301c3a4 7b$617d7350$a60 1280a@phx.gbl.. .[color=blue]
        > I am new to C# and can't get my head round what delegates
        > are and what they are for. can anyone enlighten me?[/color]

        Delegates are glorified function pointers (if that means anything to
        you). Delegates support two related concepts.

        1. Plugging in functionality into classes that are already built.
        2. Call-backs.

        An example of #1 is the Thread class. How do you tell a thread what you
        want it to do? In a world without delegates, the Thread class could have
        designed to accept a reference to an interface that defines a single method.
        The Thread object would call the single method of the interface when the
        thread was started. Of course, to create a class which implements the single
        method and then create an instance of that class to pass to the Thread
        instance seems a bit much. With delegates you can just say "Hey Thread
        object, call *this* function".

        An example of #2 is asynchronous methods. You don't want to freeze your
        app while you're waiting around for the web service method you just called
        to get back to you. No problem, you can call the web service method
        asynchronously (i.e. control returns to your app before the web service
        method completes). But how do you know when the web service method is done?
        Polling isn't very efficient (Are you done, yet?...Are you done, yet?...Are
        you...). Call-backs are the answer. You just give the asynchronous method a
        method (in your app, presumably) to call when it's done.

        Delegates also support events, which are somewhat more managed versions
        of delegates. For example, events prevent one object from inadvertently (or
        "vertantly" ) wiping out the event sinks of another. Delegates are a relative
        free-for-all.

        I hope that was somewhat lucid.

        Regards,
        Dan


        Comment

        • Empire City

          #5
          Re: Delegates

          > I am new to C# and can't get my head round what delegates[color=blue]
          > are and what they are for. can anyone enlighten me?[/color]

          I know exactly what you mean. Fortunatly I knew indirect addressing from my
          assembly language days. I also futzed about with VC++ a bit before getting
          into C#. A delegete is like a C++ function pointer. It's an indirect way of
          calling a method. For example say you didn't know the name of the method you
          wanted to call before your program started. Maybe based on your data you
          should either call a MyAddCustomerMe thod or MyDeleteCustome rMethod. You
          could pull the method name from a database and subsitute it into a delegate.
          You would then be calling your method based on your data. Here's the
          delegate tutorial:



          Look also at the simple conceptual sample under the help for delegate
          keyword.

          Lots of C# and .Net functionality uses delegates. ThreadStart, BeginInvoke,
          etc, take delegates. Events are delegates. Asynchronous programming and
          remoting use them extensively. I finally got comfortable with them after
          doing the Asynchronous programming tutorial. Once I saw that, not only can
          you use a delegate to call a method, you can also pass the actual delegate
          as a delegate to a method for use inside that method. Using the methods
          paramaters is another level of complexity in understanding them as well as
          the return type of the delegate.



          Comment

          Working...