tags:

views:

567

answers:

4

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

+3  A: 

Close and Dispose are synonymous for StreamWriter.

Joel Coehoorn
+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
technically it calls Dispose(bool) under the bonnet, a most minor nitpick though
ShuggyCoUk
you're right. I should have said it "calls dispose" without actually specifying which overload. Good spot.
Rob Levine
+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
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 the Dispose(), 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