Information Technology Reference
In-Depth Information
Another Form of the using Statement
Another form of the using statement is the following:
Keyword Resource Uses resource
using ( Expression ) EmbeddedStatement
In this form, the resource is declared before the using statement.
TextWriter tw = File.CreateText("Lincoln.txt"); // Resource declared
using ( tw ) // using statement
tw.WriteLine("Four score and seven years ago, ...");
Although this form still ensures that the Dispose method will always be called after you use
the resource, it does not protect you from attempting to use the resource after the using state-
ment has deallocated it. It therefore gives less protection and is discouraged. This form is
illustrated in Figure 9-12.
Figure 9-12. Resource declaration before the using statement
Search WWH ::




Custom Search