Double-mapped delegate

  • Thread starter Thread starter Jon Davis
  • Start date Start date
J

Jon Davis

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

myObj.MyEvent += new EventHandler(MyHandler);
myObj.MyEvent += new EventHandler(MyHandler);

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

Jon
 
Even easier to ask. ;)

Jon

Nicholas Paldino said:
Jon,

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

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

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

myObj.MyEvent += new EventHandler(MyHandler);
myObj.MyEvent += new EventHandler(MyHandler);

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

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

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

Jon
 
Back
Top