Is it poor programming practice to implement IDisposable for the purpose of cleaning up event registrations?
For example:
public void Dispose()
{
TextChanged -= TextChanged;
}
Is it poor programming practice to implement IDisposable for the purpose of cleaning up event registrations?
For example:
public void Dispose()
{
TextChanged -= TextChanged;
}
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.