Information Technology Reference
In-Depth Information
The using Statement
It is imperative that certain types of objects be allowed to clean up and free the unmanaged
resources they are holding. The using statement is used to simplify the process and ensure that
these resources are properly disposed of.
A resource is a class or struct that implements the System.IDisposable interface. Inter-
faces are covered in detail in Chapter 17—but in short, an interface is a collection of
unimplemented function members that classes and struct s can choose to implement. The
IDisposable interface contains a single method named Dispose .
The phases of using a resource are shown in Figure 9-10, and consist of the following:
￿
Allocating the resource
￿
Using the resource
￿
Disposing of the resource
If an unexpected runtime error occurs during the portion of the code using the resource,
the code disposing of the resource might not get executed.
Figure 9-10. Components of using a resource
Note The using statement is different than the using directives. The using directives are covered in
Chapter 10.
Search WWH ::




Custom Search