If you use usingstatement , the nested type should already implement IDisposable, otherwise the compiler will throw an error. Therefore, we consider the IDisposable implementation as a prerequisite for use.
using , IDisposable . , . -, , .
class MyClass : IDisposable {
public void Dispose() {
}
}
:
using (MyClass mc = new MyClass()) {
mc.DoThis();
mc.DoThat();
}
, :
MyClass mc = new MyClass();
try {
mc.DoThis();
mc.DoThat();
}
finally {
mc.Dispose();
}