1

Is it poor programming practice to implement IDisposable for the purpose of cleaning up event registrations?

For example:

public void Dispose()
{
    TextChanged -= TextChanged;
}
declouet
  • 307
  • 3
  • 10
  • No not at all a poor practice; rather a good practice if number of event registration is more. In such case, de-register them all in once place. So that, when GC invokes it can reclaim the memory space for the object. – Rahul Apr 14 '14 at 20:29

1 Answers1

2

It depends,

If you calling Dispose manually or using using statement, then it is fine.

using(var subscriber = new MySuperSubscriber()) {
   // some logic
}

If you expect Dispose to be called by finalizer - then it is not good, because finalizer can be not called, as you will have an "alive" reference to your component inside of event handler.

hazzik
  • 13,019
  • 9
  • 47
  • 86