Unattaching from an Event - Do You Need the Same Delegate Instance?

J

jehugaleahsa

Hello:

Say you have a simple class:

public class MyClass { public void DoSomething(object sender,
EventArgs e) {} }

You register the event like this:

MyClass myClass = new MyClass();

someEvent += new EventHandler(myClass.DoSomething);

Can you unregister the event like this?

someEvent -= new EventHandler(myClass.DoSomething);

or do you have to reuse the exact same instance of the delegate?

I just hate creating delegate variables just so I can unregister them
later. I am making sure they are necessary.
 
J

jehugaleahsa

[...]
Can you unregister the event like this?
someEvent -= new EventHandler(myClass.DoSomething);
or do you have to reuse the exact same instance of the delegate?

It does not have to be the same instance.  As long as the delegate is  
essentially the same -- same target object, same method overload -- any  
delegate instance will do.

In fact, usually you see code like this:

     someEvent += myClass.DoSomething;

...

     someEvent -= myClass.DoSomething;

I.e. without even an explicit instantiation of the delegate, never mind  
using the same instance.

Pete

Nice. I can't tell you how many times I've written nasty code. At some
point I must have gotten it in my head that they must be the same.

What would doing this cause?

someEvent += myClass.DoSomething;
someEvent += myClass.DoSomething;

Would the event fire DoSomething once or twice? To unregister, would
you have to -= twice, as well?

Thanks,
Travis
 
J

jehugaleahsa

[...]
Can you unregister the event like this?
someEvent -= new EventHandler(myClass.DoSomething);
or do you have to reuse the exact same instance of the delegate?

It does not have to be the same instance.  As long as the delegate is  
essentially the same -- same target object, same method overload -- any  
delegate instance will do.

In fact, usually you see code like this:

     someEvent += myClass.DoSomething;

...

     someEvent -= myClass.DoSomething;

I.e. without even an explicit instantiation of the delegate, never mind  
using the same instance.

Pete

Nice. I can't tell you how many times I've written nasty code. At some
point I must have gotten it in my head that they must be the same.

What would doing this cause?

someEvent += myClass.DoSomething;
someEvent += myClass.DoSomething;

Would the event fire DoSomething once or twice? To unregister, would
you have to -= twice, as well?

Thanks,
Travis
 
N

not_a_commie

someEvent += myClass.DoSomething;
someEvent += myClass.DoSomething;

Would the event fire DoSomething once or twice? To unregister, would
you have to -= twice, as well?

It will fire twice and you'll have to remove it twice. Remember the +=
is just a compiler shortcut to the Delegate.Combine method.
 
N

not_a_commie

someEvent += myClass.DoSomething;
someEvent += myClass.DoSomething;

Would the event fire DoSomething once or twice? To unregister, would
you have to -= twice, as well?

It will fire twice and you'll have to remove it twice. Remember the +=
is just a compiler shortcut to the Delegate.Combine method.
 
I

Ignacio Machin ( .NET/ C# MVP )

[...]
Can you unregister the event like this?
someEvent -= new EventHandler(myClass.DoSomething);
or do you have to reuse the exact same instance of the delegate?
It does not have to be the same instance.  As long as the delegate is 
essentially the same -- same target object, same method overload -- any 
delegate instance will do.
In fact, usually you see code like this:
     someEvent += myClass.DoSomething;

     someEvent -= myClass.DoSomething;
I.e. without even an explicit instantiation of the delegate, never mind 
using the same instance.

Nice. I can't tell you how many times I've written nasty code. At some
point I must have gotten it in my head that they must be the same.

What would doing this cause?

someEvent += myClass.DoSomething;
someEvent += myClass.DoSomething;

Would the event fire DoSomething once or twice? To unregister, would
you have to -= twice, as well?

Thanks,
Travis

Hi,

Why don't you try it yourself ?
It's a very straightforward test.
 
I

Ignacio Machin ( .NET/ C# MVP )

[...]
Can you unregister the event like this?
someEvent -= new EventHandler(myClass.DoSomething);
or do you have to reuse the exact same instance of the delegate?
It does not have to be the same instance.  As long as the delegate is 
essentially the same -- same target object, same method overload -- any 
delegate instance will do.
In fact, usually you see code like this:
     someEvent += myClass.DoSomething;

     someEvent -= myClass.DoSomething;
I.e. without even an explicit instantiation of the delegate, never mind 
using the same instance.

Nice. I can't tell you how many times I've written nasty code. At some
point I must have gotten it in my head that they must be the same.

What would doing this cause?

someEvent += myClass.DoSomething;
someEvent += myClass.DoSomething;

Would the event fire DoSomething once or twice? To unregister, would
you have to -= twice, as well?

Thanks,
Travis

Hi,

Why don't you try it yourself ?
It's a very straightforward test.
 
J

jehugaleahsa

On May 21, 9:13 pm, "Peter Duniho" <[email protected]>
wrote:
]
Can you unregister the event like this?
someEvent -= new EventHandler(myClass.DoSomething);
or do you have to reuse the exact same instance of the delegate?
It does not have to be the same instance.  As long as the delegate is  
essentially the same -- same target object, same method overload -- any  
delegate instance will do.
In fact, usually you see code like this:
     someEvent += myClass.DoSomething;
...
     someEvent -= myClass.DoSomething;
I.e. without even an explicit instantiation of the delegate, never mind  
using the same instance.
Pete
Nice. I can't tell you how many times I've written nasty code. At some
point I must have gotten it in my head that they must be the same.
What would doing this cause?
someEvent += myClass.DoSomething;
someEvent += myClass.DoSomething;
Would the event fire DoSomething once or twice? To unregister, would
you have to -= twice, as well?
Thanks,
Travis

Hi,

Why don't you try it yourself ?
It's a very straightforward test.- Hide quoted text -

- Show quoted text -

Because I'm lazy. ;-)
 
J

jehugaleahsa

On May 21, 9:13 pm, "Peter Duniho" <[email protected]>
wrote:
]
Can you unregister the event like this?
someEvent -= new EventHandler(myClass.DoSomething);
or do you have to reuse the exact same instance of the delegate?
It does not have to be the same instance.  As long as the delegate is  
essentially the same -- same target object, same method overload -- any  
delegate instance will do.
In fact, usually you see code like this:
     someEvent += myClass.DoSomething;
...
     someEvent -= myClass.DoSomething;
I.e. without even an explicit instantiation of the delegate, never mind  
using the same instance.
Pete
Nice. I can't tell you how many times I've written nasty code. At some
point I must have gotten it in my head that they must be the same.
What would doing this cause?
someEvent += myClass.DoSomething;
someEvent += myClass.DoSomething;
Would the event fire DoSomething once or twice? To unregister, would
you have to -= twice, as well?
Thanks,
Travis

Hi,

Why don't you try it yourself ?
It's a very straightforward test.- Hide quoted text -

- Show quoted text -

Because I'm lazy. ;-)
 

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