finally statement

finally statement block is always executed.

Therefore it is a good idea to write cleanup code in the finally block.


using System;
using System.IO;

class Test
{
    static void Main(string[] args)
    {
        StreamReader reader = null;  // In System.IO namespace 
        try
        {
            reader = File.OpenText("file.txt"); 
            if (reader.EndOfStream) 
               return; 
            Console.WriteLine(reader.ReadToEnd());
        }
        finally
        {
            if (reader != null) reader.Dispose();
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.