You can use using when the Something
class implements IDisposable. It ensures that the object is disposed correctly even if you hit an exception inside the using
block.
ie, You don't have to manually handle potential exceptions just to call Dispose
, the using
block will do it for you automatically.
It is equivalent to this:
Something mySomething = new Something();
try
{
// this is what's inside your using block
}
finally
{
if (mySomething != null)
{
mySomething.Dispose();
}
}