Dispose a StreamWriter in finally block : Finally « Language Basics « C# / CSharp Tutorial






using System;
using System.IO;

public sealed class MainClass
{
    static void Main(){
        StreamWriter sw = new StreamWriter("Output.txt");
        try {
            sw.WriteLine( "This is a test of the emergency dispose mechanism" );
        }
        finally {
            if( sw != null ) {
                ((IDisposable)sw).Dispose();
            }
        }
    }
}








1.19.Finally
1.19.1.Using finally
1.19.2.finally block is always executed even if an exception was thrown in the try
1.19.3.Dispose a StreamWriter in finally block