471,319 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

What is the point of IDisposable?

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?
Jan 31 '06 #1
9 4144
CMirandaman <CM*********@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 1 '06 #2
IDisposable is a very special interface. It is meant to be used with the
"using" key
word so it is very different than most any other interface as it has
compile time
support. It is meant for _exception safe_ deterministic cleanup of
resources.

Here is how it is done in C++/CLI
http://www.geocities.com/Jeff_Louie/...estructors.htm

Here is how it is done in C#
http://www.geocities.com/Jeff_Louie/oop26.htm

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Feb 1 '06 #3
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.

"CMirandaman" wrote:
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?

Feb 1 '06 #4
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.

"CMirandaman" <CM*********@discussions.microsoft.com> wrote in message
news:7D**********************************@microsof t.com...
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?

"Peter Rilling" wrote:
Well, as you mentioned to clean resources. The GC is non-deterministic
meaning that you have no way of knowing when it will run. If you have
finished using some objects that have external resources such as graphic
device or file handles or some other resource, if you waited for the GC
to
do its think, then you would have these resources handing around a while.
by calling dispose, your object can then be told that it no longer needs
those resources. Keep in mind that Dispose *does not* mean that the
object
itself will be removed from memory. That object still has to go through
the
GC. But internal resources can be destroyed.

Most commonly people will put a call to Dispose in their
destructor/finalize
so that resources can be cleared even if the dev forgot to explicitly
call
Dispose.

"CMirandaman" <CM*********@discussions.microsoft.com> wrote in message
news:07**********************************@microsof t.com...
> 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?


Feb 1 '06 #5
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?

"Peter Rilling" wrote:
Well, as you mentioned to clean resources. The GC is non-deterministic
meaning that you have no way of knowing when it will run. If you have
finished using some objects that have external resources such as graphic
device or file handles or some other resource, if you waited for the GC to
do its think, then you would have these resources handing around a while.
by calling dispose, your object can then be told that it no longer needs
those resources. Keep in mind that Dispose *does not* mean that the object
itself will be removed from memory. That object still has to go through the
GC. But internal resources can be destroyed.

Most commonly people will put a call to Dispose in their destructor/finalize
so that resources can be cleared even if the dev forgot to explicitly call
Dispose.

"CMirandaman" <CM*********@discussions.microsoft.com> wrote in message
news:07**********************************@microsof t.com...
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?


Feb 1 '06 #6
CMirandaman <CM*********@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 1 '06 #7
> 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.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Feb 1 '06 #8

"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)

Feb 1 '06 #9
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.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
CMirandaman <CM*********@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Feb 1 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by jim | last post: by
4 posts views Thread by Rachel Suddeth | last post: by
19 posts views Thread by Fernando Cacciola | last post: by
11 posts views Thread by Mark Rae | last post: by
3 posts views Thread by Mark | last post: by
2 posts views Thread by Henri.Chinasque | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.