Dispose a StreamWriter by using 'using' : Using Statement « File Directory Stream « C# / CSharp Tutorial






using System;
using System.IO;

public sealed class MainClass
{
    static void Main(){
        using( StreamWriter sw = new StreamWriter("Output.txt") ) {
            sw.WriteLine( "This is a test of the emergency dispose mechanism" );
        }
    }
}








15.33.Using Statement
15.33.1.Use object inside using statement
15.33.2.Create object inside the using statement
15.33.3.Using Statement: using DIRECTIVE, not using statement
15.33.4.Use FileStream inside a using statement
15.33.5.Dispose a StreamWriter by using 'using'
15.33.6.Using statement to deal with multiple resources
15.33.7.Nested using statement