Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does .Disposing a StreamWriter close the underlying stream?

Tags:

c#

.net

The StreamWriter.Close() says it also closes the underlying stream of the StreamWriter. What about StreamWriter.Dispose ? Does Dispose also dispose and/or close the underlying stream

like image 283
nos Avatar asked Jul 27 '09 11:07

nos


People also ask

Does closing a StreamReader close the underlying stream?

Yes, StreamReader , StreamWriter , BinaryReader and BinaryWriter all close/dispose their underlying streams when you call Dispose on them.

What does a StreamWriter do?

StreamWriter. Write() method is responsible for writing text to a stream. StreamWriter class is inherited from TextWriter class that provides methods to write an object to a string, write strings to a file, or to serialize XML. StreamWriter is defined in the System.IO namespace.

What does StreamWriter flush do?

Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.

What is the difference between StreamWriter and TextWriter?

The StreamWriter class in C# is used for writing characters to a stream. It uses the TextWriter class as a base class and provides the overload methods for writing data into a file. The StreamWriter is mainly used for writing multiple characters of data into a file.


1 Answers

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

Reflector is your friend for questions like this :)

like image 93
Rob Levine Avatar answered Sep 19 '22 02:09

Rob Levine