Hi all,
I am a bit confused about dispose and close methods. I will be glad if
anyone explain the difference between them to me. Do they have relation with
finalize method.
Thanks
Bhuwan
Henning Krause [MVP - Exchange] - 02 Oct 2007 06:48 GMT
Hello,
some classes implement a Close method instead of a Dispose method because it
fits better to the object being represented. In most cases, these classes
explicitly implement IDisposable. Explicitly means that you have to cast the
instance to IDisposable to get access to the Dispose method itself. The
Close method usually calls the Dispose method internally (or vice versa).
Kind regards,
Henning Krause
> Hi all,
>
[quoted text clipped - 4 lines]
> Thanks
> Bhuwan
Göran Andersson - 02 Oct 2007 10:11 GMT
> Hi all,
>
[quoted text clipped - 4 lines]
> Thanks
> Bhuwan
The Dispose method is used to tell the object to clean up everything as
it's not going to be used any more. Once Dispose has been called, the
object should not be possible to reuse.
The Close method on the other hand doesn't have to make the object
unusable. Depending on what's appropriate for the class, it may be
possible to reopen the object to reuse it.

Signature
Göran Andersson
_____
http://www.guffa.com
Jon Skeet [C# MVP] - 02 Oct 2007 19:25 GMT
> The Dispose method is used to tell the object to clean up everything as
> it's not going to be used any more. Once Dispose has been called, the
> object should not be possible to reuse.
Not necessarily. It's up to the implementation. To quote the
IDisposable docs:
<quote>
Use this method to close or release unmanaged resources such as files,
streams, and handles held by an instance of the class that implements
this interface. This method is, by convention, used for all tasks
associated with freeing resources held by an object, or preparing an
object for reuse.
</quote>
Note the last bit.
I can't say I've ever seen a type implement IDisposable in that way,
but it would be in compliance with the docs.

Signature
Jon Skeet - <skeet@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
Göran Andersson - 03 Oct 2007 20:32 GMT
>> The Dispose method is used to tell the object to clean up everything as
>> it's not going to be used any more. Once Dispose has been called, the
[quoted text clipped - 15 lines]
> I can't say I've ever seen a type implement IDisposable in that way,
> but it would be in compliance with the docs.
Right. I didn't think of that special use. :)
I've never seen it used that way either. It would of course only make
sense if the object can be in a state where it either can be reused or
discarded. Also, as a finalizer is usually used as a backup when the
object uses unmanaged resources, and the Dispose method then calls
GC.SuppressFinalize to remove it from the finalization queue, it's
pretty much only useful for objects that don't use any unmanaged resources.

Signature
Göran Andersson
_____
http://www.guffa.com
James Jardine - 02 Oct 2007 13:20 GMT
> Hi all,
>
[quoted text clipped - 4 lines]
> Thanks
> Bhuwan
A good example of the difference is the SqlConnection class. This class has
both a Close and a Dispose. The close only closes the connection and
affects nothing else in the class. THe dispose method actually calls the
close method automatically and also cleans up any other resources used by
the class. If a class has a Dispose method it is recommended to call it
when you are done with the object.
Thanks,
James Jardine
Edgar Harris - 02 Oct 2007 18:04 GMT
In most cases classes that have a Close method implement IDisposable
explicitly. In those cases there is no difference between calling close or
Dispose. For example the following two code snippets are identical:
string fileContents = null;
using (StreamReader reader = new StreamReader("MyFile")
{
fileContents = reader.ReadToEnd();
}
string fileContents = null;
StreamReader reader = new StreamReader("MyFile");
try
{
fileContents = reader.ReadToEnd();
}
finally
{
fileContents.Close();
}
In the first snippet I used a using statement which will call Dispose on the
StreamReader, and in the second snippet I used a try finally and called Close
in the try finally; since StreamReader's Dispose just calls its Close method
there are no differences in the code.
> Hi all,
>
[quoted text clipped - 4 lines]
> Thanks
> Bhuwan
Edgar Harris - 02 Oct 2007 18:27 GMT
Oops. I guess there is one major difference between the snippets, one
compiles and the other does not. Here is a fix for the first snippet.
string fileContents = null;
using (StreamReader reader = new StreamReader("MyFile") )
{
fileContents = reader.ReadToEnd();
}
> In most cases classes that have a Close method implement IDisposable
> explicitly. In those cases there is no difference between calling close or
[quoted text clipped - 30 lines]
> > Thanks
> > Bhuwan
Bhuwan Bhaskar - 02 Oct 2007 19:19 GMT
Thanks all!
Bhuwan
> Hi all,
>
[quoted text clipped - 4 lines]
> Thanks
> Bhuwan
Phil Wilson - 03 Oct 2007 18:32 GMT
A framework person once told me it was just history and timing. A lot of
classes were already coded using Close methods before the IDisposable model
became the standard. That implies you shouldn't see Close() on any new
classes.

Signature
Phil Wilson
[MVP Windows Installer]
> Hi all,
>
[quoted text clipped - 4 lines]
> Thanks
> Bhuwan