What is the point of IDisposable?

G

Guest

Perhaps someone can help me understand this. I'm trying to understand what
the IDisposable interface actually does because as far as I can tell it seems
to be just garnish on the plate.

If I create a Dispose method to clean up resources I have to either call it
explicitly or call it in the destructor to make it run. I've tried it both
with and without implementing IDisposable and it doesn't seem to make a
difference to the GC. Either way, my Dispose doesn't seem to get called
unless I have a specific reference to it somewhere in my code. So what's the
point of IDisposable?
 
G

Guest

Peter,

Thanks for the response but this is just my point. I can implement
everything you described by creating a "Dispose" method on a class but
nothing in the architecture seems to require that I implement IDisposable on
that class. What does IDisposable do that a regular method cannot?
 
K

Kevin Spencer

IDisposable is not a method. It's an interface. An interface is like a
contract. And what it does is tell anyone who wants to know (such as an
application) that there is something that needs to be disposed, and because
it defines the method signature, the client knows how to dispose it.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.
 
J

Jon Skeet [C# MVP]

CMirandaman said:
Thanks for the response but this is just my point. I can implement
everything you described by creating a "Dispose" method on a class but
nothing in the architecture seems to require that I implement IDisposable on
that class. What does IDisposable do that a regular method cannot?

As well as Kevin's reply, it's part of the pattern for the "using"
statement, so I can write:

using (Stream x = new FileStream (...))
{
...
}

and Dispose will be called automatically. An interface is the best way
of ensuring that the compiler knows there will be a method called
Dispose and that its purpose will be to clean up resources.
 
G

Guest

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.

Thanks.
 
J

Jon Skeet [C# MVP]

CMirandaman said:
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.

Well, even without language support it would be useful, because it
indicates at a glance that the class should be disposed of, and the
point of the Dispose method.
 
?

=?iso-8859-1?Q?Lasse=20V=e5gs=e6ther=20Karlsen?=

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.

IDisposable is also a way for your class to signal to generic (not the 2.0
generic) code that your objects require cleanup before being left alone in
that big void the memory is.

For instance, you can build a collection that supports IDisposable and when
you call .Dispose() on it, it could go in and check each object in the collection
and call .Dispose() on that object, if it supports IDisposable. You could
not (easily/accurately) do that if you didn't tag the class as implementing
IDisposable.
 
N

Nick Hounsome

CMirandaman said:
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)
 
K

Kevin Spencer

Good point, Jon. Of course, it goes hand-in-hand with my answer, as the
using method depends upon the interface being there in order to work. But
using "using" is a really handy way to cut down on code, and prevent errors.
Certainly a "best practice."

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top