Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0200 using statement

We can use the using statement to simplify the finally block and try ... catch statement. The following: using (StreamReader reader = File.OpenText ("file.txt")) { ... } is precisely equivalent to: StreamReader reader = File.OpenText ("file.txt"); try { ... } finally { if (reader != null) ((IDisposable)reader).Dispose(); }