is it necessary to unsubscribe from events?

This is the important part from the MSDN documentation that you should take into consideration

To prevent your event handler from being invoked when the event is raised, simply unsubscribe from the event. In order to prevent resource leaks, it is important to unsubscribe from events before you dispose of a subscriber object. Until you unsubscribe from an event, the multicast delegate that underlies the event in the publishing object has a reference to the delegate that encapsulates the subscriber's event handler. As long as the publishing object holds that reference, your subscriber object will not be garbage collected.


It IS important to unsubscribe from events. If you do not, then the subscriber cannot be garbage collected leading to -- in essence -- a memory leak. Here is a good example of the problems you may run into if you do not unsubscribe:

http://developers.slashdot.org/article.pl?sid=07/11/17/0552247

Also, it could lead to performance problems as the event handler will continue to be called even though it is not doing anything useful for you anymore.

On the other hand, if you're just ending the execution of the program, then there is no reason to unsubscribe from events. It is certainly not mandatory, and I don't see any reason to recommend it.


It depends how long the subscriber and publisher live. Here is an in depth article about the problem and several approaches on how to solve it here: Solving the Problem with Events: Weak Event Handlers

Tags:

C#

Events

Memory