I read the excellent answer explaining how to use the Dispose pattern, and also why it works that way.
Proper use of the IDisposable interface
The post clearly states that you would want to use the Dispose pattern in two different scenarios:
- get rid of unmanaged resources (because we have to)
- get rid of managed resources (because we want to be helpful)
My question is:
- When an object is subscribed to an external event for its entire lifetime, is it also common/good practice to unregister from that event in the Dispose method? Would you implement the IDisposable interface for that purpose?
Yes, you should.
This is the best way to indicate to consumers of your class that it has “resources” that must be released. (even though event subscriptions are technically not resources)