The using Statement


In addition to the using directive that was previously encountered, using has a second form that is called the using statement. The general forms of a using statement are:

using (obj)
{
  // insert code here
}

using (type obj=initializer)
{
  // insert code here
}

where obj is an object that is being used inside the using block. In the first form, the object is declared elsewhere; whereas, in the second form, the object is being declared inside the using statement. When the block concludes, the method Dispose() (defined by the interface System.IDisposable) is called. A using statement only applies to objects that implement System.IDisposable.