Need a double flash?

Is it necessary to rinse a Stream after rinsing a StreamWriter ?

 public static async Task WriteStringAsync(this Stream stream, string messageString) { var encoding = new UTF8Encoding(false); //no BOM using (var streamWriter = new StreamWriter(stream, encoding)) { await streamWriter.WriteAsync(messageString); await streamWriter.FlushAsync(); } await stream.FlushAsync(); //is this necessary? } 
+6
source share
2 answers

According to MSDN docs, this can be forgiven as "just making sure" ...

StreamWriter.Flush ():

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

Stream.Flush ():

When overridden in a derived class, clears all buffers for this stream and forces any buffered data to be written to the base device.

... However, a closer look at the code in TypeDescriptor shows that StreamWriter.Flush () (and I would suggest its asynchronous counterpart to FlushAsync) is an overload that calls the main function, passing in two Boolean parameters that instruct StreamWriter to clear Stream and Unicode encoder. So, one call to StreamWriter.FlushAsync (), combined with the await keyword, to make sure the async operation has occurred completely, should be fine.

+4
source

No need, the methods StreamWriter.Flush and StreamWriter.FlushAsync internally call Stream.Flush .

+4
source

All Articles