Double-mapped delegate

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

    #1

    Double-mapped delegate

    If two delegates are created that point to the exact same method, and an
    event is assigned both delegates, ...

    myObj.MyEvent += new EventHandler(My Handler);
    myObj.MyEvent += new EventHandler(My Handler);

    .... does it recognize the duplicate method and ignore the second one, or
    does it execute the same method twice when raised?

    Jon


  • Lee Alexander

    #2
    Re: Double-mapped delegate

    You'll delegate called twice.

    Regards
    Lee
    "Jon Davis" <jon@REMOVE.ME. PLEASE.jondavis .net> wrote in message
    news:uDhGoIQUDH A.2188@TK2MSFTN GP10.phx.gbl...[color=blue]
    > If two delegates are created that point to the exact same method, and an
    > event is assigned both delegates, ...
    >
    > myObj.MyEvent += new EventHandler(My Handler);
    > myObj.MyEvent += new EventHandler(My Handler);
    >
    > ... does it recognize the duplicate method and ignore the second one, or
    > does it execute the same method twice when raised?
    >
    > Jon
    >
    >[/color]


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Double-mapped delegate

      Jon,

      Just a side note, this is very easy to test...

      --
      - Nicholas Paldino [.NET/C# MVP]
      - nicholas.paldin o@exisconsultin g.com

      "Jon Davis" <jon@REMOVE.ME. PLEASE.jondavis .net> wrote in message
      news:uDhGoIQUDH A.2188@TK2MSFTN GP10.phx.gbl...[color=blue]
      > If two delegates are created that point to the exact same method, and an
      > event is assigned both delegates, ...
      >
      > myObj.MyEvent += new EventHandler(My Handler);
      > myObj.MyEvent += new EventHandler(My Handler);
      >
      > ... does it recognize the duplicate method and ignore the second one, or
      > does it execute the same method twice when raised?
      >
      > Jon
      >
      >[/color]


      Comment

      • Jon Davis

        #4
        Re: Double-mapped delegate

        Even easier to ask. ;)

        Jon

        "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com> wrote
        in message news:u5cwWeRUDH A.2648@TK2MSFTN GP10.phx.gbl...[color=blue]
        > Jon,
        >
        > Just a side note, this is very easy to test...
        >
        > --
        > - Nicholas Paldino [.NET/C# MVP]
        > - nicholas.paldin o@exisconsultin g.com
        >
        > "Jon Davis" <jon@REMOVE.ME. PLEASE.jondavis .net> wrote in message
        > news:uDhGoIQUDH A.2188@TK2MSFTN GP10.phx.gbl...[color=green]
        > > If two delegates are created that point to the exact same method, and an
        > > event is assigned both delegates, ...
        > >
        > > myObj.MyEvent += new EventHandler(My Handler);
        > > myObj.MyEvent += new EventHandler(My Handler);
        > >
        > > ... does it recognize the duplicate method and ignore the second one, or
        > > does it execute the same method twice when raised?
        > >
        > > Jon
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Jon Davis

          #5
          Re: Double-mapped delegate

          >> You can check whether a delegate contains a given event handler by
          iterating
          over the return value of GetInvocationLi st and using the overridden equality
          operator (==) to check. <<

          This is an excellent and very relevant answer, Simon, thank you very much!

          Jon


          "Simon Trew" <ten.egnaro@wer ts> wrote in message
          news:uxBH$nSUDH A.1868@TK2MSFTN GP11.phx.gbl...[color=blue]
          > The Remarks section of http://tinyurl.com/hsmt (MSDN Delegate.Combin e
          > documentation) states:
          >
          > "The invocation list can contain duplicate entries; that is, entries that
          > refer to the same method on the same object."
          >
          > The "Return value" section of http://tinyurl.com/hsne (MSDN[/color]
          Delegate.Remove[color=blue]
          > documentation) states:
          >
          > If the invocation list of value matches a contiguous set of elements in[/color]
          the[color=blue]
          > invocation list of source, then the invocation list of value is said to
          > occur within the invocation list of source. If the invocation list of[/color]
          value[color=blue]
          > occurs more than once in the invocation list of source, the last[/color]
          occurrence[color=blue]
          > is removed.
          >
          > So also, you will need to remove (using -= in C#) the same number of
          > duplicates as you added.
          >
          > You can check whether a delegate contains a given event handler by[/color]
          iterating[color=blue]
          > over the return value of GetInvocationLi st and using the overridden[/color]
          equality[color=blue]
          > operator (==) to check.
          >
          > e.g.
          >
          > bool DelegateContain sHandler(Delega te delegate, EventHandler handler)
          > {
          > bool bFound = false;
          > foreach (Delegate d in MyObj.MyEvent.G etInvocationLis t())
          > {
          > bFound = d == handler;
          > if (bFound) break;
          > }
          > return bFound;
          > }
          >
          >
          >
          > "Jon Davis" <jon@REMOVE.ME. PLEASE.jondavis .net> wrote in message
          > news:uDhGoIQUDH A.2188@TK2MSFTN GP10.phx.gbl...[color=green]
          > > If two delegates are created that point to the exact same method, and an
          > > event is assigned both delegates, ...
          > >
          > > myObj.MyEvent += new EventHandler(My Handler);
          > > myObj.MyEvent += new EventHandler(My Handler);
          > >
          > > ... does it recognize the duplicate method and ignore the second one, or
          > > does it execute the same method twice when raised?
          > >
          > > Jon
          > >
          > >[/color]
          >
          >[/color]


          Comment

          Working...