The
IDisposable.Dispose
is used in one more case: when you need to control some action called at the moment of disposable. The moment of the destructor call is (in regular code not tricking GC) is normally out of your control.
In .NET, the rule of thumb is not writing destructors at all; there are exclusions, of course.
Example: you have some object which you create, call one method of it which takes a second or two then it is not needed anymore; and want to do it on UI thread. You need to make sure the application shows Hourglass cursor while application is busy with this method. There is a little problem here: you do not to forget to set this cursor back; you also don't want to get stuck with the Hourglass cursor is an exception is thrown.
Here is the technique: create object controlling mouse pointer: 1) in constructor, set up hourglass mouse pointer; 2) make it disposable, in Dispose, setup default mouse pointer or a pointer remembered at the moment of the constructor call, then use:
using(WaitCursorIndicator controller = new WaitCursorIndicator(myControl) {
}
Please look at my Tips/Tricks article for detailed explanation:
Hourglass Mouse Cursor Always Changes Back to its Original Image. How?[
^]
On the end of using block, default mouse pointer will be restored, even on exception. There are a lot of similar techniques like that.
Typically, IDisposable is useful if you take benefits of using block or
if you need a chain of dispose action going to the very top level, such as a form. (Criteria listed in previous answer should apply.)