Multicast delegate checking for particular delegate

E

Edward Diener

I am surprised that a MulticastDelegate 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 MulticastDelegate.
 
J

Jon Skeet [C# MVP]

Edward Diener said:
I am surprised that a MulticastDelegate 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 MulticastDelegate.

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

Edward Diener

Jon said:
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...

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 MulticastDelegate function which only
adds a delegate handler if that delegate handler does not already exist in
its invocation list might be useful.
 
J

Jon Skeet [C# MVP]

Edward Diener said:
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.

Well, in some cases it's not. In some cases it's exactly what you want.
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 MulticastDelegate function which only
adds a delegate handler if that delegate handler does not already exist in
its invocation list might be useful.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top