Possible duplicate:
Using IDisposable to unsubscribe
I found that I implement IDisposable for each class that processes events that come from objects not declared inside the class. For instance:
public class Text
{
public Text(ClassWithEvents c)
{
c.Event += EventHandler;
}
}
In this situation, I declare the Text class as IDisposable, and in the Dispose method I removed the event handler to avoid binding the object to memory and other unpleasant things (for example, executing code on objects that must be dead).
I was wondering if there is a better way to do this, because I don't like having too many one-time classes, because they need to be "handled with care."