Dispose - , , "Dispose". , (, Windows). IDisposable , . , , , , , IDisposable.
:
var myClass = MyDisposableClass();
myClass.Dispose();
Proper usage:
using (var myClass = MyDisposableClass())
{
}
, :
MyDisposableClass myClass = MyDisposableClass();
try
{
// do stuff with myClass
}
finally
{
myClass.Dispose();
}
The important difference is that no matter what happens, you know that your Dispose will be invoked. Alternatively, you can bind a destructor (which, if one exists, is called by the garbage collector), which you can then bind to call the Dispose method; but if you need to do this for any reason, be sure not to release the same resource twice (set your pointers to zero after releasing).
source
share