PDA

Click to See Complete Forum and Search --> : EvenHandlers -= question. Delegates are reference based?


BramVandenbon
Mar 24th, 2007, 05:40 AM
Hi,

For once and for all I would like to clear up something for myself because I have been wondering about this for a long time.

I have seen several people add eventhandlers in the following way, and so do I:

Something instance = new Something();
instance.event += new EventHandler(somefunction);

now let's say you wish to replace the instance at a later point in your application. You would want the eventhandler to be linked with the new instance. Besides that you would want to remove the link with the original instance to avoid memoryleaks. In code samples I have often seen the following:

instance.event -= new EventHandler(somefunction);
instance = new Something();
instance.event += new EventHandler(somefunction);

When I first saw it I had a lot of criticism. This will be ok if EventHandler is a struct, but it will be a mistake if EventHandler is a class. Because in that case the 2nd new EventHandler(...) and the 1st new EventHandler(...) represent different instances so you would be removing nothing at all. However EventHandler is not a class and it is not a structure, it is a delegate.

So my main question is: is a delegate a reference based thing or is it placed immediatly on the stack like a structure? If it is a reference than I believe the above should be handled as followed to be correct:

//the first time storing the handler in a variable
Something instance = new Something();
EventHandler tmpHandler = new EventHandler(somefunction);
instance.event += tmpHandler;

//so that later the same variable can be removed
instance.event -= tmpHandler;
instance = new Something();
instance.event += new EventHandler(somefunction);

Should I use this? Or is the simple event -= new Eventhandler(...) ok?

I hope somebody who had the same question and found some proof to back up one of the 2 theories can share his discoveries with me.

Thank you :)

jmcilhinney
Mar 24th, 2007, 08:54 AM
Delegates are reference type objects, like like classes. With that in mind, you would think that your second code snippet should not be effective. In fact it is because event handlers are treated in a special way. It is impractical to expect people to code like your third snippet. When you do this:instance.event -= new EventHandler(somefunction);you might expect that C# tries to remove that specific event handler from the event. What actually happens is that C# checks all the event handlers assigned to that event and removes the first one it finds that refers to the same method. If none are found then none are removed. If multiple exist then only the first is removed.

BramVandenbon
Mar 26th, 2007, 02:08 AM
Thank you very much for clearing this up :).

BramVandenbon
Mar 26th, 2007, 02:10 AM
I would love to Add some to your reputation but vbforum tells me I should spread some reputation around before giving it to you again :-D lol.