Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

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