The StreamWriter.Close() says it also closes the underlying stream of the StreamWriter. What about StreamWriter.Dispose ? Does Dispose also dispose and/or close the underlying stream
+10
A:
StreamWriter.Close() just calls StreamWriter.Dispose() under the bonnet, so they do exactly the same thing. StreamWriter.Dispose() does close the underlying stream.
Reflector is your friend for questions like this :)
Rob Levine
2009-07-27 11:47:17
technically it calls Dispose(bool) under the bonnet, a most minor nitpick though
ShuggyCoUk
2009-07-27 12:27:01
you're right. I should have said it "calls dispose" without actually specifying which overload. Good spot.
Rob Levine
2009-07-27 13:14:05
+4
A:
From StreamWriter.Close()
public override void Close()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
From TextWriter.Dispose() (which StreamWriter inherits)
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
They are thus, identical.
ShuggyCoUk
2009-07-27 11:47:41
A:
To quote from Framework Design Guidelines by Cwalina and Abrams in the section about the dispose pattern:
CONSIDER providing method
Close(), in addition to theDispose(), if close is standard terminology in the area.
Apparently Microsoft follow their own guidelines, and assuming this is almost always a safe bet for the .NET base class library.
Martin Liversage
2009-07-27 12:14:30