"CMirandaman" <CM*********@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
OK, now I get it. IDisposable is required for the using syntax but not
really
needed if you only expect to call the Dispose method explicitely or in the
destructor.
Note that you used the word "expect". You might expect that it will always
be called explicitly but why prevent future developers of your code from
using it in a "using" statement.
The advantage of the using statement over explicit code is that it handles
exceptions. I f you don't use it then you typically need to use
try...finally to ensure that resources are not retained longer than necessay
and this is much more ugly.
There is also another magic use of IDisposable in the case of enumerator in
a foreach statement but this is not neeeded as often.
Another reason that your class should use IDisposable is for when it is
contained in another class with several other disposable objects. - Often
the cleanest way to ensure cleanup of everything is to store it all in a
collection and then loop through the collection looking for things that
implement IDisposable - I f every class just has its own Close or Done or
Finish method this is not possible (this is the reason for the components
field that VS adds to your forms)