Does Stream.Dispose always call Stream.Close (and Stream.Flush)?

It is safe to assume that if an object implements IDispose, that it will dispose of itself properly If it doesn't then that would be a bug If not, then is the following just bad code: No, that code is the recommended way of dealing with objects that implement IDispose.

Yes, thats what it's for. Are there any Stream implimentations that don't work as expected (Like CryptoStream)? It is safe to assume that if an object implements IDispose, that it will dispose of itself properly.

If it doesn't then that would be a bug. If not, then is the following just bad code: No, that code is the recommended way of dealing with objects that implement IDispose.

I used Reflector and found that System.IO.Stream. Dispose looks like this: public void Dispose() { this.Close(); }.

As Daniel Bruckner mentioned, Dispose and Close are effectively the same thing. However Stream does NOT call Flush() when it is disposed/closed. FileStream (and I assume any other Stream with a caching mechanism) does call Flush() when disposed.

If you are extending Stream, or MemoryStream etc. You will need to implement a call to Flush() when disposed/closed if it is necessary.

I was looking into this recently, and if you're extending MemoryStream, you probably don't need to worry about flushing. Looking at the implementation of Flush for MemoryStream indicates that it's actually a no-op, in fact the documentation states "Overrides Flush so that no action is performed. ".

– Mike Goatly Jun 23 at 9:19 @Mike, if a stream is writing directly to memory with no buffering then flush is unnecessary, if a stream introduces some buffering on top of an otherwise un-buffered stream type a call to flush will be needed. – ScottS Jun 23 at 17:45 Can't argue with that - sorry for digging up a 2 year old post! :) – Mike Goatly Jun 23 at 21:08.

Stream. Close is implemented by a call to Stream. Dispose or vice versa - so the methods are equivalent.Stream.

Close exists just because closing a stream sounds more natural than disposing a stream. Besides you should try to avoid explicit calls to this methods and use the using statement instead in order to get correct exception handling for free.

All standard Streams (FileStream, CryptoStream) will attempt to flush when closed/disposed. I think you can rely on this for any Microsoft stream implementations. As a result, Close/Dispose can throw an exception if the flush fails.In fact IIRC there was a bug in the .

NET 1.0 implementation of FileStream in that it would fail to release the file handle if the flush throws an exception. This was fixed in . NET 1.1 by adding a try/finally block to the Dispose(boolean) method.

Both StreamWriter.Dispose() and Stream.Dispose() release all resources held by the objects. Both of them close the underlying stream. The source code of Stream.Dispose() (note that this is implementation details so don't rely on it): public void Dispose() { this.Close(); } StreamWriter.Dispose() (same as with Stream.Dispose()): protected override void Dispose(bool disposing) { try { // Not relevant things } finally { if (this.

Closable && (this. Stream! = null)) { try { if (disposing) { this.stream.Close(); } } finally { // Not relevant things } } } } Still, I usually implicitly close streams/streamwriters before disposing them - I think it looks cleaner.

For objects that need to be manually closed, every effort should be made to create the object in a using block. //Cannot access 'stream' using (FileStream stream = File. Open ("c:\\test.

Bin")) { //Do work on 'stream' } // 'stream' is closed and disposed of even if there is an exception escaping this block // Cannot access 'stream' In this way one can never incorrectly access 'stream' out of the context of the using clause and the file is always closed.

I used Reflector and found that System.IO.Stream. Dispose looks like this.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions