Do you need to call Flush() on a stream or writer if you are using the “using” statement?

Ben picture Ben · Oct 10, 2011 · Viewed 12.2k times · Source

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)?

Answer

Davide Piras picture Davide Piras · Oct 10, 2011

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.