What is the difference between Dispose and close methods.

B

Bhuwan Bhaskar

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
 
H

Henning Krause [MVP - Exchange]

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
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Bhuwan said:
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

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.
 
J

James Jardine

Bhuwan Bhaskar said:
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

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
 
G

Guest

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.
 
G

Guest

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();
}
 
J

Jon Skeet [C# MVP]

Göran Andersson said:
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.
 
P

Phil Wilson

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.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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

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.
 

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