Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of streamwriters having Close() and Dispose()?

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?

like image 980
Pharap Avatar asked Dec 27 '12 16:12

Pharap


People also ask

Does StreamWriter dispose close the stream?

Close() just calls StreamWriter. Dispose() under the bonnet, so they do exactly the same thing. StreamWriter. Dispose() does close the underlying stream.

Do you have to Close StreamWriter?

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.

What is the difference between close and dispose in VB net?

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.

Do I need to dispose Streamreader?

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.


2 Answers

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.

like image 70
Servy Avatar answered Oct 20 '22 00:10

Servy


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.

like image 35
mellamokb Avatar answered Oct 20 '22 01:10

mellamokb