There are two different uses of the using keyword in C#:
- To declare implicit package scope, for example "using System;"
- To cause the Dispose() method of an IDisposable object to be called.
In the latter case, using(myVar) {}
is shorthand for:
IDisposable disposable = (IDisposable)myVar;
try
{
// your code here
}
finally
{
if (disposable != null)
disposable.Dispose();
}