Removing events more than once

  • Thread starter Thread starter MikeP
  • Start date Start date
M

MikeP

Hi there,

I've tried hunting for this in the C# specs but can't find the relevant
info. Does anyone know what the official rule says (or doesn't say) about
removing an event more than once. If you apply "+=" to assign an event for
instance, can you safely apply "-=" more than once or is this implementation
dependent (and possibly dangerous). Thanks very much
 
Hi,

MikeP said:
Hi there,

I've tried hunting for this in the C# specs but can't find the relevant
info. Does anyone know what the official rule says (or doesn't say) about
removing an event more than once. If you apply "+=" to assign an event for
instance, can you safely apply "-=" more than once or is this
implementation dependent (and possibly dangerous). Thanks very much

I haven't read the documentation, but if they used the same pattern than for
the rest of the list I can tell you that it does not matter, that it will
removed if found and nothing happens if it does not.
 
Hello Ignacio Machin ( .NET/ C# MVP )" machin TA laceupsolutions.com,

Yep, exactly.

C# Spec. 15.4
Attempting to remove a delegate from an empty list (or to remove a non-existent
delegate from a non-empty list) is not an error

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


I> Hi,
I>
I> I>I> I haven't read the documentation, but if they used the same pattern
I> than for the rest of the list I can tell you that it does not matter,
I> that it will removed if found and nothing happens if it does not.
I>
 
Hello Ignacio Machin ( .NET/ C# MVP )" machin TA laceupsolutions.com,

Yep, exactly.

C# Spec. 15.4
Attempting to remove a delegate from an empty list (or to remove a non-existent
delegate from a non-empty list) is not an error

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


I> Hi,
I>
I> I>I> I haven't read the documentation, but if they used the same pattern
I> than for the rest of the list I can tell you that it does not matter,
I> that it will removed if found and nothing happens if it does not.
I>
 
Mike,

As Ignacio says, the behavior is that nothing will happen.

Furthermore, looking through the C# spec, I would say it is undefined
what the behavior is.

However, I would strongly recommend that you keep track of this, as you
should know when event handlers need to be added and removed. I would
almost classify it as a design bug when you don't (the same way I would
classify not knowing when your classes that implement IDisposable need to be
disposed of).
 
Bleh, I didn't see section 15.4 which Michael pointed out.


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

Nicholas Paldino said:
Mike,

As Ignacio says, the behavior is that nothing will happen.

Furthermore, looking through the C# spec, I would say it is undefined
what the behavior is.

However, I would strongly recommend that you keep track of this, as you
should know when event handlers need to be added and removed. I would
almost classify it as a design bug when you don't (the same way I would
classify not knowing when your classes that implement IDisposable need to
be disposed of).


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

MikeP said:
Hi there,

I've tried hunting for this in the C# specs but can't find the relevant
info. Does anyone know what the official rule says (or doesn't say) about
removing an event more than once. If you apply "+=" to assign an event
for instance, can you safely apply "-=" more than once or is this
implementation dependent (and possibly dangerous). Thanks very much
 
Hello Ignacio Machin ( .NET/ C# MVP )" machin TA laceupsolutions.com,
Yep, exactly.

C# Spec. 15.4
Attempting to remove a delegate from an empty list (or to remove a
non-existent delegate from a non-empty list) is not an error

Thanks very much to you and the others. It's strange however that the
section number you referred to (15.4) is actually 15.3 in the online version
of MSDN I'm now looking at. These section numbers don't correspond at all
however to the official downloaded spec from Sept/2006 but I think there may
be two versions floating around (for the ECMA and ISO perhaps). It's in
section 22.3 of the latter document anyway (on page 367 or 383 if you're
looking at the Adobe page number). The quote there however is:

"Attempting to subtract a delegate from null (or to subtract a non-existent
delegate from a non-empty list) is not an error".

In any case, I was really asking because sometimes you may want to assign
and remove an event in the course of processing a given function. If you
remove it in a "finally" clause however, and you jump through that clause
because of an exception, it's possible that you'll be removing the event
even though it was never assigned (because the exception occurred before
that took place). While you could organize things so this doesn't happen,
it's just not convenient sometimes, in particular when you're cleaning up
different resources (including other events possibly - the code can start to
become unwieldy if you introduce multiple "finally" and/or "catch" blocks if
you can get away with just one). Anyway, thanks again.
 
MikeP said:
[...]
In any case, I was really asking because sometimes you may want to assign
and remove an event in the course of processing a given function.

I agree that there are a number of situations in which removing without
knowing whether you've added would be a reasonable and useful design.

The one caveat I would stipulate is to take note that you can add the
same handler more than once. I haven't personally seen a situation in
which this is necessarily a good design, but I'm not willing to say that
it could never happen. In that case, removing a handler without knowing
specifically that it's been added could result in an incorrect number of
subscriptions by that handler to the event.

Pete
 
In any case, I was really asking because sometimes you may want to assign
I agree that there are a number of situations in which removing without
knowing whether you've added would be a reasonable and useful design.

The one caveat I would stipulate is to take note that you can add the same
handler more than once. I haven't personally seen a situation in which
this is necessarily a good design, but I'm not willing to say that it
could never happen. In that case, removing a handler without knowing
specifically that it's been added could result in an incorrect number of
subscriptions by that handler to the event.

Yes, I'm aware of that but it's not an issue in my case. Actually, this is
where I miss C++ (with no intention of starting a religious war). You can
simply rely on RAII to clean things up.
 
if u are removing events more than once it will not show any
compiletime/runtime error it will work properly .
but variable will refenence to null.
show before calling uneed to check that event varaible is not null
others it will through exceptiin object refence not set

event delegatevaraible e;
e+= new delegatevaraible (functionname);

e-= new delegatevaraible (functionname);
e-= new delegatevaraible (functionname);
// till now it willwork
// now u need to check
if(e!=null)
{
e.invoke(paramete)
}
 

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

Back
Top