I am not sure whether I need to call Flush()
on the used objects if I write something like this:
using (FileStream...) using (CryptoStream...) using (BinaryWriter...) { // do something }
Are they always automatically flushed? When does the using
statement flush them and when it doesn’t (if that can happen)?
The resources are automatically closed when using try-with-resource block. As part of this process it will also invoke flush automatically.
Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.
You don't need to use Flush on the MemoryStream , as it's not buffering anything that is written to any other source. There is simply nothing to flush anywhere. The Flush method is only present in the MemoryStream object because it inherits from the Stream class.
As soon as you leave the using block’s scope, the stream is closed and disposed. The Close() calls the Flush(), so you should not need to call it manually.
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