views:

685

answers:

3

Can't remember where I read it, but I remember a MS guy saying that once you create a reader around a System.IO.Stream, then the stream is not longer responsible for the disposal of the stream.

Is this true? Can someone confirm this? Maybe provide a reference.

This means the outer using in this code is redundant

using (var s = new FileStream(@"c:\file.txt",FileMode.Open)) {
    using (var reader = new StreamReader(s)) {
         reader.ReadToEnd();
    }
}
+2  A: 

read the approved answer to this question:

http://stackoverflow.com/questions/674879/who-disposes-of-an-idisposable-public-property/675001#675001

It says that StreamReader always disposes the underlying stream.

Igor Zelaya
This will allow concise constructs like using (var reader = new StreamReader(new FileStream(@"c:\file.txt",FileMode.Open))) { reader.ReadToEnd(); }
Cheeso
+2  A: 

Both FileStream and StreamReader implement IDisposable, so I would use both using's regardless, that way if the underlying implementation were to change, however unlikely, your code would still be good.

Mitch Wheat
StreamReader.ReadToEnd is fine - it's inherited from TextReader.
Jon Skeet
+1. This will result in Stream.Dispose being called twice, but IDisposable.Disposable implementations should ignore all calls after the first. Thus you can and generally should always call Dispose on a disposable class.
Joe
+1  A: 

If isn't redundant, for the reason that the "new StreamReader(s)" line could fail (throw an exception). That would mean that no using block is going to Dispose() the stream. In reality, this is unlikely... but it could! For example, the stream might be non-readable (or whatever).

Thus, it is always best to guard such initialization; far better to be disposed twice than not at all. Plus it makes it obvious to the reader (and any code analysis tools) that you have definitely cleaned up after yourself.

Marc Gravell
If the construction of the StreamReader throws, the using of the Reader isn't entered yet, so it won't Dispose and it doesn't have to.The outer using is active but it will check s != null and not Dispose either. And that is correct too.
Henk Holterman
No - you're missing the point; the OP asks "is the outer using redundant" - no: it isn't. If you take it away, then if the StreamReader throws during creation then the FileStream will *not* be disposed. Only finalized, eventually.
Marc Gravell