Dispose a StreamWriter in finally block : Finally : Language Basics C# Examples


C# Examples » Language Basics » Finally »

 

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




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Language Basics
» Finally