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 :)
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 :)