To enable a class to be used under a using() block, the class must implement the IDisposable interface with its own implementation of the Dispose() method.
public class MyClass : IDisposable {
public void Dispose() {
// implementation of Dispose method
}
}
// in some other method
using(var myclass = new MyClass()) {
// myclass instance is alive inside
// this block only
}
Dispose is never called by the .NET Framework; you must call it manually – preferably by wrapping its creation in a using() block.