Flush() and Close()

  • Thread starter Thread starter guy
  • Start date Start date
G

guy

I can't find this in the documentation so I'm asking it here:

I assume that if I call Close() on a Stream based class that Flush() does
not need to be called before as it is implicitly called by Close()?

Thanks
 
That is correct.

When you close the stream any remaining buffered content is written out at
that point, otherwise that data would be lost.

Chris.
 
guy said:
I can't find this in the documentation so I'm asking it here:

I assume that if I call Close() on a Stream based class that Flush() does
not need to be called before as it is implicitly called by Close()?

Yup.

For most streams (CryptoStream excepted due to a bug) you can just call
Dispose instead, which is much easier with a using statement:

using (Stream x = ...)
{
....
} // Dispose called automatically here
 
Hi guy,

Calling System.IO.Stream.Close on a Stream flushes any buffered data,
essentially calling System.IO.Stream.Flush for you. System.IO.Stream.Close
also releases operating system resources such as file handles, network
connections, or memory used for any internal buffering.

Nirosh.
 
Back
Top