Delegates

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

    #1

    Delegates

    im having problems trying to understand the delegates in
    c#, does anybody know some link where i can find a good
    and simple explanation?

    thanks
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Delegates

    kode,

    Check out the section of the .NET framework documentation titled
    "Delegates Tutorial", located at (watch for line wrap):

    http://msdn.microsoft.com/library/de...estutorial.asp

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "kode" <anonymous@disc ussions.microso ft.com> wrote in message
    news:00ca01c399 7e$068f00b0$a30 1280a@phx.gbl.. .[color=blue]
    > im having problems trying to understand the delegates in
    > c#, does anybody know some link where i can find a good
    > and simple explanation?
    >
    > thanks[/color]


    Comment

    • would b hacker

      #3
      Re: Delegates

      there's also a pretty good chapter in Jesse Liberty's C#
      book, which just happens to be available FREE ONLINE
      http://www.oreilly.com/catalog/progc...hapter/ch12.pd
      f

      cheers!

      btw his discussion of why events have the accessors they
      do is dumb, but that's another story..

      [color=blue]
      >-----Original Message-----
      >kode,
      >
      > Check out the section of the .NET framework[/color]
      documentation titled[color=blue]
      >"Delegates Tutorial", located at (watch for line wrap):
      >
      >http://msdn.microsoft.com/library/default.asp?[/color]
      url=/library/en-us/csref/html/vcwlkdelegatest utorial.asp[color=blue]
      >
      > Hope this helps.
      >
      >
      >--
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      >"kode" <anonymous@disc ussions.microso ft.com> wrote in[/color]
      message[color=blue]
      >news:00ca01c39 97e$068f00b0$a3 01280a@phx.gbl. ..[color=green]
      >> im having problems trying to understand the delegates[/color][/color]
      in[color=blue][color=green]
      >> c#, does anybody know some link where i can find a good
      >> and simple explanation?
      >>
      >> thanks[/color]
      >
      >
      >.
      >[/color]

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Delegates

        would b hacker <allanc@digital word.net> wrote:[color=blue]
        > btw his discussion of why events have the accessors they
        > do is dumb, but that's another story..[/color]

        What's "dumb" about it? Makes perfect sense to me.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • wood bee hacker

          #5
          Re: Delegates

          the problem with the event model is that it offers an add-
          remove syntax which prevents retrieving any delegates
          that have been assigned to the event. this means that
          you can't retrieve event handlers in reflection, for
          instance you can't copy event handlers from one object to
          another.

          according to liberty, this is to prevent objects with
          access to the event (Main in liberty's example) from
          impersonating the event (Main causing the printing of a
          fictitious time on the console). this kind of issue is
          silly, of coures main can do all kinds of wierd stuff if
          it really wants, requiring that main not hijack events
          should be a matter of style not language grammar.

          btw does anybody know if there is a pre-c# history to the
          event model that c# simply copied, or is the add/remove
          event model novel with c#?

          [color=blue]
          >-----Original Message-----
          >would b hacker <allanc@digital word.net> wrote:[color=green]
          >> btw his discussion of why events have the accessors[/color][/color]
          they[color=blue][color=green]
          >> do is dumb, but that's another story..[/color]
          >
          >What's "dumb" about it? Makes perfect sense to me.
          >
          >--
          >Jon Skeet - <skeet@pobox.co m>
          >http://www.pobox.com/~skeet
          >If replying to the group, please do not mail me too
          >.
          >[/color]

          Comment

          • Eric Gunnerson [MS]

            #6
            Re: Delegates

            The concern is more about accidental problems as opposed to malicious
            intent. If you could assign directly, you'd be able to write:

            appDomain.Domai nLoadEvent = new EventHandler(ro utine);

            If somebody was already hooked up to the event, you would have just unhooked
            them. Which would be bad.
            --
            Eric Gunnerson

            Visit the C# product team at http://www.csharp.net
            Eric's blog is at http://blogs.gotdotnet.com/ericgu/

            This posting is provided "AS IS" with no warranties, and confers no rights.
            "wood bee hacker" <allanc@digital word.net> wrote in message
            news:02a701c399 94$77562650$a30 1280a@phx.gbl.. .[color=blue]
            > the problem with the event model is that it offers an add-
            > remove syntax which prevents retrieving any delegates
            > that have been assigned to the event. this means that
            > you can't retrieve event handlers in reflection, for
            > instance you can't copy event handlers from one object to
            > another.
            >
            > according to liberty, this is to prevent objects with
            > access to the event (Main in liberty's example) from
            > impersonating the event (Main causing the printing of a
            > fictitious time on the console). this kind of issue is
            > silly, of coures main can do all kinds of wierd stuff if
            > it really wants, requiring that main not hijack events
            > should be a matter of style not language grammar.
            >
            > btw does anybody know if there is a pre-c# history to the
            > event model that c# simply copied, or is the add/remove
            > event model novel with c#?
            >
            >[color=green]
            > >-----Original Message-----
            > >would b hacker <allanc@digital word.net> wrote:[color=darkred]
            > >> btw his discussion of why events have the accessors[/color][/color]
            > they[color=green][color=darkred]
            > >> do is dumb, but that's another story..[/color]
            > >
            > >What's "dumb" about it? Makes perfect sense to me.
            > >
            > >--
            > >Jon Skeet - <skeet@pobox.co m>
            > >http://www.pobox.com/~skeet
            > >If replying to the group, please do not mail me too
            > >.
            > >[/color][/color]


            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Delegates

              wood bee hacker <allanc@digital word.net> wrote:[color=blue]
              > the problem with the event model is that it offers an add-
              > remove syntax which prevents retrieving any delegates
              > that have been assigned to the event. this means that
              > you can't retrieve event handlers in reflection, for
              > instance you can't copy event handlers from one object to
              > another.[/color]

              Which is good in many ways - if a class *wants* to expose which
              handlers are being used, it can do so, but it doesn't *have* to.
              [color=blue]
              > according to liberty, this is to prevent objects with
              > access to the event (Main in liberty's example) from
              > impersonating the event (Main causing the printing of a
              > fictitious time on the console). this kind of issue is
              > silly, of coures main can do all kinds of wierd stuff if
              > it really wants, requiring that main not hijack events
              > should be a matter of style not language grammar.[/color]

              No, because it helps to stop mistakes happening, just like strong
              typing, access modifiers and the like. Would you suggest that
              everything should be public as well? It's the same kind of argument.
              [color=blue]
              > btw does anybody know if there is a pre-c# history to the
              > event model that c# simply copied, or is the add/remove
              > event model novel with c#?[/color]

              Don't know, to be honest.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • news.microsoft.com

                #8
                Re: Delegates

                I can still app hijack with PostMessage and subclassing outside the runtime.


                "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                news:MPG.1a02ad 85a129c4ca98992 8@msnews.micros oft.com...[color=blue]
                > wood bee hacker <allanc@digital word.net> wrote:[color=green]
                > > the problem with the event model is that it offers an add-
                > > remove syntax which prevents retrieving any delegates
                > > that have been assigned to the event. this means that
                > > you can't retrieve event handlers in reflection, for
                > > instance you can't copy event handlers from one object to
                > > another.[/color]
                >
                > Which is good in many ways - if a class *wants* to expose which
                > handlers are being used, it can do so, but it doesn't *have* to.
                >[color=green]
                > > according to liberty, this is to prevent objects with
                > > access to the event (Main in liberty's example) from
                > > impersonating the event (Main causing the printing of a
                > > fictitious time on the console). this kind of issue is
                > > silly, of coures main can do all kinds of wierd stuff if
                > > it really wants, requiring that main not hijack events
                > > should be a matter of style not language grammar.[/color]
                >
                > No, because it helps to stop mistakes happening, just like strong
                > typing, access modifiers and the like. Would you suggest that
                > everything should be public as well? It's the same kind of argument.
                >[color=green]
                > > btw does anybody know if there is a pre-c# history to the
                > > event model that c# simply copied, or is the add/remove
                > > event model novel with c#?[/color]
                >
                > Don't know, to be honest.
                >
                > --
                > Jon Skeet - <skeet@pobox.co m>
                > http://www.pobox.com/~skeet
                > If replying to the group, please do not mail me too[/color]


                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Delegates

                  news.microsoft. com <anonymouse@dis cussions.micros oft.com> wrote:[color=blue]
                  > I can still app hijack with PostMessage and subclassing outside the runtime.[/color]

                  Sure. It's not absolutely preventing you from doing horrible thing -
                  but it's making life easier for those of us who don't *want* to do
                  nasty things, but would make mistakes if it were really easy to do
                  those things.

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • news.microsoft.com

                    #10
                    Re: Delegates

                    Its something .NET should have addressed. An unsecure app message queue.


                    "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                    news:MPG.1a02e2 6ce564ff3098992 a@msnews.micros oft.com...[color=blue]
                    > news.microsoft. com <anonymouse@dis cussions.micros oft.com> wrote:[color=green]
                    > > I can still app hijack with PostMessage and subclassing outside the[/color][/color]
                    runtime.[color=blue]
                    >
                    > Sure. It's not absolutely preventing you from doing horrible thing -
                    > but it's making life easier for those of us who don't *want* to do
                    > nasty things, but would make mistakes if it were really easy to do
                    > those things.
                    >
                    > --
                    > Jon Skeet - <skeet@pobox.co m>
                    > http://www.pobox.com/~skeet
                    > If replying to the group, please do not mail me too[/color]


                    Comment

                    • Jon Skeet [C# MVP]

                      #11
                      Re: Delegates

                      news.microsoft. com <discussion@dis cussion.microso ft.com> wrote:[color=blue]
                      > Its something .NET should have addressed. An unsecure app message queue.[/color]

                      But that's completely unrelated to whether or not the idea of events is
                      good or not.

                      --
                      Jon Skeet - <skeet@pobox.co m>
                      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                      If replying to the group, please do not mail me too

                      Comment

                      • wood bee hacker

                        #12
                        Re: Delegates

                        i agree, with caveat below, and you raise a very
                        reasonable concern-- but also one that i would address
                        very simply. seems to me that the correct way to enable
                        reflection would be to change the "add/remove" paradigm
                        to "add/remove/get". then for instance to do a copy you
                        would use get followed by a += loop. your accidental
                        problem below wouldnt happen because there is no "set".
                        you might not even call the new accessor "get" but rather
                        a cast of the event to a MulticastDelega te.

                        now the caveat: the current model actually DOES allow
                        you to write the accidental errant code exactly as you
                        just proposed-- provided 2 conditions are met. First,
                        your errant code must appear in the class which declares
                        the event rather than in a different class-- admittedly
                        not a common scenario since it is normally external
                        objects which register event handlers. Second, the event
                        must have been declared using the (more common) variable-
                        declarators form rather than the member-name (accessor)
                        form.

                        voila: i kid you not-- try it! if you write the
                        statement in one class you get a compiler error, in
                        another class you don't (even though the declaration is
                        marked "public"). if you declare the event using one
                        syntax you get a compiler error, using the alternate
                        syntax you don't. now i bet you didnt know that, and i
                        bet most programmers don't know that, so i submit that if
                        you dont agree with me that the event grammar is bogus,
                        you should at least agree that it is OBSCURE.

                        back to working with reality the way it is, do you happen
                        to know who actually designed this into the c# grammar?
                        are there any discussion threads or anybody who would
                        remember what they were actually thinking or where they
                        got their model or inspiration from? i am indeed
                        curious!!


                        [color=blue]
                        >-----Original Message-----
                        >The concern is more about accidental problems as opposed[/color]
                        to malicious[color=blue]
                        >intent. If you could assign directly, you'd be able to[/color]
                        write:[color=blue]
                        >
                        >appDomain.Doma inLoadEvent = new EventHandler(ro utine);
                        >
                        >If somebody was already hooked up to the event, you[/color]
                        would have just unhooked[color=blue]
                        >them. Which would be bad.
                        >--
                        >Eric Gunnerson
                        >
                        >Visit the C# product team at http://www.csharp.net
                        >Eric's blog is at http://blogs.gotdotnet.com/ericgu/
                        >
                        >This posting is provided "AS IS" with no warranties, and[/color]
                        confers no rights.[color=blue]
                        >"wood bee hacker" <allanc@digital word.net> wrote in[/color]
                        message[color=blue]
                        >news:02a701c39 994$77562650$a3 01280a@phx.gbl. ..[color=green]
                        >> the problem with the event model is that it offers an[/color][/color]
                        add-[color=blue][color=green]
                        >> remove syntax which prevents retrieving any delegates
                        >> that have been assigned to the event. this means that
                        >> you can't retrieve event handlers in reflection, for
                        >> instance you can't copy event handlers from one object[/color][/color]
                        to[color=blue][color=green]
                        >> another.
                        >>
                        >> according to liberty, this is to prevent objects with
                        >> access to the event (Main in liberty's example) from
                        >> impersonating the event (Main causing the printing of a
                        >> fictitious time on the console). this kind of issue is
                        >> silly, of coures main can do all kinds of wierd stuff[/color][/color]
                        if[color=blue][color=green]
                        >> it really wants, requiring that main not hijack events
                        >> should be a matter of style not language grammar.
                        >>
                        >> btw does anybody know if there is a pre-c# history to[/color][/color]
                        the[color=blue][color=green]
                        >> event model that c# simply copied, or is the add/remove
                        >> event model novel with c#?
                        >>
                        >>[color=darkred]
                        >> >-----Original Message-----
                        >> >would b hacker <allanc@digital word.net> wrote:
                        >> >> btw his discussion of why events have the accessors[/color]
                        >> they[color=darkred]
                        >> >> do is dumb, but that's another story..
                        >> >
                        >> >What's "dumb" about it? Makes perfect sense to me.
                        >> >
                        >> >--
                        >> >Jon Skeet - <skeet@pobox.co m>
                        >> >http://www.pobox.com/~skeet
                        >> >If replying to the group, please do not mail me too
                        >> >.
                        >> >[/color][/color]
                        >
                        >
                        >.
                        >[/color]

                        Comment

                        • Jon Skeet [C# MVP]

                          #13
                          Re: Delegates

                          wood bee hacker <allanc@digital word.net> wrote:[color=blue]
                          > i agree, with caveat below, and you raise a very
                          > reasonable concern-- but also one that i would address
                          > very simply. seems to me that the correct way to enable
                          > reflection would be to change the "add/remove" paradigm
                          > to "add/remove/get". then for instance to do a copy you
                          > would use get followed by a += loop. your accidental
                          > problem below wouldnt happen because there is no "set".
                          > you might not even call the new accessor "get" but rather
                          > a cast of the event to a MulticastDelega te.[/color]

                          If you want to provide that functionality, I don't believe there's
                          anything stopping you from doing so - you can write your own code to
                          store the event handlers, and then provide a way of accessing them.
                          [color=blue]
                          > now the caveat: the current model actually DOES allow
                          > you to write the accidental errant code exactly as you
                          > just proposed-- provided 2 conditions are met. First,
                          > your errant code must appear in the class which declares
                          > the event rather than in a different class-- admittedly
                          > not a common scenario since it is normally external
                          > objects which register event handlers. Second, the event
                          > must have been declared using the (more common) variable-
                          > declarators form rather than the member-name (accessor)
                          > form.
                          >
                          > voila: i kid you not-- try it![/color]

                          No need to, I believe you.
                          [color=blue]
                          > if you write the
                          > statement in one class you get a compiler error, in
                          > another class you don't (even though the declaration is
                          > marked "public").[/color]

                          And that's pretty reasonable, given the intention. I don't think it's
                          that odd to automatically give the class itself rather more control
                          over the event.
                          [color=blue]
                          > if you declare the event using one
                          > syntax you get a compiler error, using the alternate
                          > syntax you don't. now i bet you didnt know that, and i
                          > bet most programmers don't know that, so i submit that if
                          > you dont agree with me that the event grammar is bogus,
                          > you should at least agree that it is OBSCURE.[/color]

                          It's obscure, but you won't run into it unless you're trying to do
                          something that very few people will want or need to do. I don't have
                          much of a problem with that. I also don't *think* it's guaranteed by
                          the C# spec - it's due to the way that the MS C# compiler *happens* to
                          pick the same variable name for the field as for the event (rather than
                          __eventName as the C# spec example suggests as a possibility). *That*
                          is a bit of a pity, I agree. (In that people may start relying on it
                          when they shouldn't.)
                          [color=blue]
                          > back to working with reality the way it is, do you happen
                          > to know who actually designed this into the c# grammar?
                          > are there any discussion threads or anybody who would
                          > remember what they were actually thinking or where they
                          > got their model or inspiration from? i am indeed
                          > curious!![/color]

                          I don't know of any discussions about it - but then I arrived fairly
                          late on the C# scene.

                          --
                          Jon Skeet - <skeet@pobox.co m>
                          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                          If replying to the group, please do not mail me too

                          Comment

                          • wood bee hacker

                            #14
                            Re: Delegates

                            thanks jon, especially for the insight about fields v
                            events!

                            here's a simple thing i want to do, is there a simple or
                            standard way to do it? i want to VSDesigner a template
                            TreeView object and copy it at runtime to a new TreeView
                            () object. i can copy the properties no problem using
                            reflection, how do i copy the events?

                            known but tacky solutions are: manually += the events in
                            code rather than Designer (yuk), or wrap TreeView and
                            manually supply the functionality as you mention
                            previously (awkward, and breaks if TreeView changes).

                            thanks in advance!

                            [color=blue]
                            >-----Original Message-----
                            >wood bee hacker <allanc@digital word.net> wrote:[color=green]
                            >> i agree, with caveat below, and you raise a very
                            >> reasonable concern-- but also one that i would address
                            >> very simply. seems to me that the correct way to[/color][/color]
                            enable[color=blue][color=green]
                            >> reflection would be to change the "add/remove"[/color][/color]
                            paradigm[color=blue][color=green]
                            >> to "add/remove/get". then for instance to do a copy[/color][/color]
                            you[color=blue][color=green]
                            >> would use get followed by a += loop. your accidental
                            >> problem below wouldnt happen because there is[/color][/color]
                            no "set".[color=blue][color=green]
                            >> you might not even call the new accessor "get" but[/color][/color]
                            rather[color=blue][color=green]
                            >> a cast of the event to a MulticastDelega te.[/color]
                            >
                            >If you want to provide that functionality, I don't[/color]
                            believe there's[color=blue]
                            >anything stopping you from doing so - you can write your[/color]
                            own code to[color=blue]
                            >store the event handlers, and then provide a way of[/color]
                            accessing them.[color=blue]
                            >[color=green]
                            >> now the caveat: the current model actually DOES allow
                            >> you to write the accidental errant code exactly as you
                            >> just proposed-- provided 2 conditions are met. First,
                            >> your errant code must appear in the class which[/color][/color]
                            declares[color=blue][color=green]
                            >> the event rather than in a different class--[/color][/color]
                            admittedly[color=blue][color=green]
                            >> not a common scenario since it is normally external
                            >> objects which register event handlers. Second, the[/color][/color]
                            event[color=blue][color=green]
                            >> must have been declared using the (more common)[/color][/color]
                            variable-[color=blue][color=green]
                            >> declarators form rather than the member-name[/color][/color]
                            (accessor)[color=blue][color=green]
                            >> form.
                            >>
                            >> voila: i kid you not-- try it![/color]
                            >
                            >No need to, I believe you.
                            >[color=green]
                            >> if you write the
                            >> statement in one class you get a compiler error, in
                            >> another class you don't (even though the declaration[/color][/color]
                            is[color=blue][color=green]
                            >> marked "public").[/color]
                            >
                            >And that's pretty reasonable, given the intention. I[/color]
                            don't think it's[color=blue]
                            >that odd to automatically give the class itself rather[/color]
                            more control[color=blue]
                            >over the event.
                            >[color=green]
                            >> if you declare the event using one
                            >> syntax you get a compiler error, using the alternate
                            >> syntax you don't. now i bet you didnt know that, and[/color][/color]
                            i[color=blue][color=green]
                            >> bet most programmers don't know that, so i submit that[/color][/color]
                            if[color=blue][color=green]
                            >> you dont agree with me that the event grammar is[/color][/color]
                            bogus,[color=blue][color=green]
                            >> you should at least agree that it is OBSCURE.[/color]
                            >
                            >It's obscure, but you won't run into it unless you're[/color]
                            trying to do[color=blue]
                            >something that very few people will want or need to do.[/color]
                            I don't have[color=blue]
                            >much of a problem with that. I also don't *think* it's[/color]
                            guaranteed by[color=blue]
                            >the C# spec - it's due to the way that the MS C#[/color]
                            compiler *happens* to[color=blue]
                            >pick the same variable name for the field as for the[/color]
                            event (rather than[color=blue]
                            >__eventName as the C# spec example suggests as a[/color]
                            possibility). *That*[color=blue]
                            >is a bit of a pity, I agree. (In that people may start[/color]
                            relying on it[color=blue]
                            >when they shouldn't.)
                            >[color=green]
                            >> back to working with reality the way it is, do you[/color][/color]
                            happen[color=blue][color=green]
                            >> to know who actually designed this into the c#[/color][/color]
                            grammar?[color=blue][color=green]
                            >> are there any discussion threads or anybody who would
                            >> remember what they were actually thinking or where[/color][/color]
                            they[color=blue][color=green]
                            >> got their model or inspiration from? i am indeed
                            >> curious!![/color]
                            >
                            >I don't know of any discussions about it - but then I[/color]
                            arrived fairly[color=blue]
                            >late on the C# scene.
                            >
                            >--
                            >Jon Skeet - <skeet@pobox.co m>
                            >http://www.pobox.com/~skeet
                            >If replying to the group, please do not mail me too
                            >.
                            >[/color]

                            Comment

                            • Jon Skeet [C# MVP]

                              #15
                              Re: Delegates

                              wood bee hacker <allanc@digital word.net> wrote:[color=blue]
                              > thanks jon, especially for the insight about fields v
                              > events!
                              >
                              > here's a simple thing i want to do, is there a simple or
                              > standard way to do it? i want to VSDesigner a template
                              > TreeView object and copy it at runtime to a new TreeView
                              > () object. i can copy the properties no problem using
                              > reflection, how do i copy the events?[/color]

                              Well, I'd suggest using Clone, but that doesn't exist for TreeView. Of
                              course, you could derive from TreeView yourself, and call
                              Object.Memberwi seClone from there, but then I suspect your event
                              handlers would be really tied to each other.
                              [color=blue]
                              > known but tacky solutions are: manually += the events in
                              > code rather than Designer (yuk), or wrap TreeView and
                              > manually supply the functionality as you mention
                              > previously (awkward, and breaks if TreeView changes).[/color]

                              I don't know of any other way, to be honest.

                              --
                              Jon Skeet - <skeet@pobox.co m>
                              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                              If replying to the group, please do not mail me too

                              Comment

                              Working...