Multicast delegate checking for particular delegate

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

    #1

    Multicast delegate checking for particular delegate

    I am surprised that a MulticastDelega te does not have a particular member
    function which checks if a particular delegate is in its invocation list. I
    can do this manually of course but a Find, or something like that, should
    have been a member function of the MulticastDelega te.


  • Jon Skeet [C# MVP]

    #2
    Re: Multicast delegate checking for particular delegate

    Edward Diener <eddielee@tropi csoft.com> wrote:[color=blue]
    > I am surprised that a MulticastDelega te does not have a particular member
    > function which checks if a particular delegate is in its invocation list. I
    > can do this manually of course but a Find, or something like that, should
    > have been a member function of the MulticastDelega te.[/color]

    I don't think it's the kind of thing that's used very often, to be
    honest. I certainly have never needed it myself, and I can't remember
    anyone ever asking for it before...

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

    • Edward Diener

      #3
      Re: Multicast delegate checking for particular delegate

      Jon Skeet [C# MVP] wrote:[color=blue]
      > Edward Diener <eddielee@tropi csoft.com> wrote:[color=green]
      >> I am surprised that a MulticastDelega te does not have a particular
      >> member function which checks if a particular delegate is in its
      >> invocation list. I can do this manually of course but a Find, or
      >> something like that, should have been a member function of the
      >> MulticastDelega te.[/color]
      >
      > I don't think it's the kind of thing that's used very often, to be
      > honest. I certainly have never needed it myself, and I can't remember
      > anyone ever asking for it before...[/color]

      It could be used to make sure that a delegate referencing the same member
      function is not added twice or removed if it is not already there. The
      former is more important as I assume attempting to remove a delegate which
      does not already exist does nothing. However adding the same member function
      twice is probably not wise.

      It might be argued that any code which could add the same handler twice is
      flawed in it design, and that may be so. When you replied I re-checked my
      code that I was writing to prevent that and realized that if my logic was
      correct, it could not happen by design. However I can imagine situations
      where a Find might be useful, or a MulticastDelega te function which only
      adds a delegate handler if that delegate handler does not already exist in
      its invocation list might be useful.


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Multicast delegate checking for particular delegate

        Edward Diener <eddielee@tropi csoft.com> wrote:[color=blue][color=green]
        > > I don't think it's the kind of thing that's used very often, to be
        > > honest. I certainly have never needed it myself, and I can't remember
        > > anyone ever asking for it before...[/color]
        >
        > It could be used to make sure that a delegate referencing the same member
        > function is not added twice or removed if it is not already there. The
        > former is more important as I assume attempting to remove a delegate which
        > does not already exist does nothing. However adding the same member function
        > twice is probably not wise.[/color]

        Well, in some cases it's not. In some cases it's exactly what you want.
        [color=blue]
        > It might be argued that any code which could add the same handler twice is
        > flawed in it design, and that may be so. When you replied I re-checked my
        > code that I was writing to prevent that and realized that if my logic was
        > correct, it could not happen by design. However I can imagine situations
        > where a Find might be useful, or a MulticastDelega te function which only
        > adds a delegate handler if that delegate handler does not already exist in
        > its invocation list might be useful.[/color]

        The easiest way of doing that is probably just to remove it first, to
        be honest.

        myDelegate -= foo;
        myDelegate += foo;

        Admittedly that won't reduce the number if it's been added more than
        once already...

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