Mega Code Archive

 
Categories / C# Tutorial / Language Basics
 

Dispose a StreamWriter in finally block

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();             }         }     } }