StreamReader class has both close and dispose method. I want to know which method to call to clean up all resources.
If making use of using block, I think it will call its dispose method. Will it be enough to clean up all the resources.
The main difference between Close and Dispose in the case of SqlConnectionObject is: An application can call Close more than one time. No exception is generated. If you called Dispose method SqlConnection object state will be reset.
All replies. Close() will remove the connection from the database. Dispose() removes the connection from the database and calls the internal cleanup of the object to remove reference to all of its variables. Dispose() removes the connection from the database and calls the internal cleanup...
There is a simple guideline: if you have finished with an object whose type implements IDisposable then call Dispose() on it; you should use a using block to do this.
Dispose improves performance and optimizes memory by releasing unmanageable objects and scarce resources, like Graphics Device Interface (GDI) handles used in applications with restricted Windows space. The Dispose method, provided by the IDisposable interface, implements Dispose calls.
The using
block will call Dispose()
on the StreamReader
instance. Generally speaking, if a type is IDisposable
, you should put it in using
scope.
EDIT: If you look at the Close()
implementation of StreamReader
using Reflector, you will see that it's calling Dispose(true)
. So if you're not using the using
scope, calling Close()
manually would be the same as calling Dispose()
in this particular case.
protected override void Dispose(bool disposing) { try { if ((this.Closable && disposing) && (this.stream != null)) { this.stream.Close(); } } finally { if (this.Closable && (this.stream != null)) { this.stream = null; this.encoding = null; this.decoder = null; this.byteBuffer = null; this.charBuffer = null; this.charPos = 0; this.charLen = 0; base.Dispose(disposing); } } }
We all know System.IO.StreamReader
is not the only .NET 4.0+ class that implements IDisposable
and a Close()
method. For the case of StreamReader
in this question, the source code shows that the base class TextReader.Close()
, TextReader.Dispose()
both run the same lines of code. You can also see in the code that TextReader.Dispose()
is the implementation for when you call StreamReader.Dispose()
(because StreamReader
doesn't override that method overload signature of Dispose
).
So a call to StreamReader.Dispose()
will run this inherited line of code, which calls the protected override method StreamReader.Dispose(disposing: true)
and so will StreamReader.Close()
call StreamReader.Dispose(disposing: true)
. So for the case of StreamReader
, Close()
and Dispose()
do happen to run the same lines of code.
A more general, non-class-specific answer to the question of Close() or Dispose()?, might be to note that Microsoft has fairly clear documentation on implementing IDisposable
and the Dispose pattern. A quick read is enough to show you that implementing a Close()
method is not a requirement of the Dispose pattern.
imho the reason for finding the method Close()
on so many classes that implement IDisposable
, is the result of convention, not requirement.
Someone commented
Close and Dispose - which to call?
An example of another class that implements IDisposable
with the Dispose pattern, and has a Close()
method. Does Close()
run the same code as Dispose()
in this case? I haven't looked at the source code, but I'd say not necessarily.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With