When you call Close
on an active StreamWriter
it makes it impossible to write any more code to the stream (since it's been closed). To open another stream, you have to make a new instance of a StreamWriter
since there's no 'Open' method.
My question is, what's the point in having Close
and Dispose
when you can't really use anything besides Dispose
after closing the stream?
I could understand if there was an Open
function, i.e. you could close one file then open another using the same StreamWriter
. But as there is only Close
and you can't really use anything besides Dispose
afterwards, why not just get rid of Close
and have Dispose
close the underlying stream as its first action?
I get that Dispose
comes from IDisposeable
and all that. What I want to know is why Close
is needed specifically when Dispose
appears to call Close
anyway.
As far as I can see, without the ability to open another stream with the same StreamWriter
, there is no point in having Close
when you have no option but to Dispose
afterwards since all other methods become useless.
Why is is that StreamWriter
bothers having Close
when they could merge Close
and Dispose
into a single method?
Close() just calls StreamWriter. Dispose() under the bonnet, so they do exactly the same thing. StreamWriter. Dispose() does close the underlying stream.
You must call Close to ensure that all data is correctly written out to the underlying stream. Following a call to Close, any operations on the StreamWriter might raise exceptions.
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.
Always call Dispose before you release your last reference to the TextReader. Otherwise, the resources it is using will not be freed until the garbage collector calls the TextReader object's Finalize method.
When dealing with streams there's a long standing convention that Close
is a method that they have to close the stream. It's terminology that many programmers are used to and expect to see when dealing with streams. It would have been potentially confusing to have a stream without a Close
method, and may have taken some time for people to realize that they should use Dispose
to close the stream.
It's certainly possible for the class to have implemented IDisposable
explicitly, so that the Dispose
method wouldn't be there without a cast to IDisposable
, and that my have cleaned up some of the confusion, but they choose not to do that and to leave two duplicate methods in the class.
It's entirely up to your personal preference whether you use Dispose
or Close
to close the stream.
According to the documentation, Close
just calls Dispose
with a value of true
.
This method overrides Close.
This implementation of Close calls the Dispose method passing a true value.
You must call Close to ensure that all data is correctly written out to the underlying stream. Following a call to Close, any operations on the StreamWriter might raise exceptions. If there is insufficient space on the disk, calling Close will raise an exception.
Dispose
itself is inherited from IDisposable
, which is used by classes that have resources that need to be released.
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