Click here to Skip to main content
15,889,604 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone!
I have some question about "using" block in C# for resource cleanup...
I'm reading C# books and each author gives examples on "using":
using (StreamWriter sw = new StreamWriter(@"C:\sample.txt") )
{
      sw.WriteLine("some info");
      sw.WriteLine("blah - blah");|
}


That's it ... they didn't use sw.Close() method !!!!!!!!!!!!!!!!!!!!!!!!!
I know that in "using" statement evrything is Disposed, even if exceptions occur....
but anyway there must be a mean to close a stream so it could, for example, add EOF symbols to the end of the file or something ........
in "using" block such happens:
finally
{
    if ( sw != null)
        ((IDisposable)sw).Dispose();
}

So it places "sw" object to the dispose queue so GarbageCollector will delete it later.....
so all that time program would have access to my physical drive .........
I don't want that ... what if some other exceptions from other objects occur so then it would not dispose my "sw" object cause the program would be interrupted......
...And even if it normally disposes "sw" object ....will it close it ??.......will it add EOF symbol to mark the end of file????
Please explain.....
Posted
Updated 20-Mar-10 11:55am
v2

You can use reflector or a similar tool to see what the Dispose method does, I would suggest it would almost certainly close a stream that is open, the reason it needs a dispose at all is because there's a physical resource to be cleaned up, and it's the file handle, which means making sure it is closed.

Here is the Dispose method of StreamWriter

C#
protected override void Dispose(bool disposing)
{
    try
    {
        if ((this.stream != null) && (disposing || (!this.Closable && (this.stream is __ConsoleStream))))
        {
            this.Flush(true, true);
            if (this.mdaHelper != null)
            {
                GC.SuppressFinalize(this.mdaHelper);
            }
        }
    }
    finally
    {
        if (this.Closable && (this.stream != null))
        {
            try
            {
                if (disposing)
                {
                    this.stream.Close();
                }
            }
            finally
            {
                this.stream = null;
                this.byteBuffer = null;
                this.charBuffer = null;
                this.encoding = null;
                this.encoder = null;
                this.charLen = 0;
                base.Dispose(disposing);
            }
        }
    }
}


Notice the calls to Flush() and Close()
 
Share this answer
 
v2
use sw.Flush() method to flush all memory resources
use sw.Close() to close writer instance
use sw.Dispose() method to dispose it

regards
koolprasad2003 :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900