I have an application that runs continuously, it creates and destroys classes some of which have events like mouse click events and the like…
First question is what is the proper way to unsubscribe? If the subscribe looks like this:
Panel1.MouseClick += new MouseEventHandler(Action_MouseClick);
is it proper to unsubscribe like this:
Panel1.MouseClick -= new MouseEventHandler(Action_MouseClick);
OR is it ok to do this:
Panel1.MouseClick -= Action_MouseClick;
or is either way ok?
My other question is is If I use the Microsoft Visual C# studio to create the events through the designer, does it automatically unsubscribe as part of the ‘Dispose’ method? Or do I still need to put the unsubscribe method in the code?
Either way of unsubscribing will have the same effect, and both are correct.
As for your other question .. if you use the designer to create events for controls on a form, when the form is disposed the source of the events no longer exists, so they won’t be called. I guess I’m saying it isn’t necessary to detach those events.