This is 100% the correct way. If a class leverages IDisposable
it should be wrapped in a using
statement to ensure that the Dispose()
method is called. Further, communicating with an outside technology -unmanaged at that -like SQL Server shouldn’t be taken lightly. The SqlCommand
object implements IDisposable
for a very good reason. The code below is the Dispose()
method for the SqlCommand
object:
protected override void Dispose(bool disposing)
{
if (disposing)
{
this._cachedMetaData = null;
}
base.Dispose(disposing);
}
and as you can see, it’s releasing a reference to the _cachedMetaData
object so that it too can get cleaned up.