473,508 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4256
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5790
by: jim | last post by:
Why was a destructor (finalizer) included in the syntax of C# if implementing IDisposable is the sure way to release resources held by a class? If you were doing a language from scratch would you...
4
39935
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
3
3482
by: Dave | last post by:
I trying to determine the best pattern for designing my business and data layers... Can the instance of the business object eventually cause memory leaks in Example 1? If your business class...
19
1752
by: Fernando Cacciola | last post by:
I'm puzzled, Why and how _exactly_ is this: void Foo<T>(T v ) where T : Interface/Value/Class/class any better than this void Foo( Interface/Value/Class/object v )
11
2292
by: Mark Rae | last post by:
Hi, Following on from the recent thread about why HttpWebRequest doesn't implement the IDisposable interface, I got to wondering whether people make their custom classes inherit IDisposable or...
3
2131
by: Mark | last post by:
If your class implements IDisposable, I was told that this increases the speed with which your class is garbage collected. Is this true? And if so, how much "time" does it save? Assume that we...
4
9682
by: cgarcia0117 | last post by:
For any class I write in C# that has a member variable that implements IDisposable my class implements the IDisposable pattern. I do this to guarantee the reference to the member is explicitly...
2
11760
by: Henri.Chinasque | last post by:
I have a feeling this is a dumb one, but here it is: IEnumerator<Timplements IDisposable, but the thing is I'm not sure what I'm supposed to dispose! I'm also curious why IEnumerator<T>...
1
2888
by: Jules Winfield | last post by:
Friend, I have a class that contains some data. The data will be accessed from multiple threads of which 90% are reading and 10% are writing. A private ReaderWriterLockSlim object is used...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.