在 IDisposable class 中处理对象
Object dispose in IDisposable class
我只是像这样在 class 中继承 IDisposable 接口。
public class Program3:IDisposable
{
}
上面class创建实例时,我想手动销毁对象还是自动销毁对象?
注意:我没有明确使用 Dispose 方法来处置对象
您必须实现 IDisposable 接口方法:
public void Dispose()
{
// Clear all unmanaged resources
}
无论何时实例化对象,都应该在 using 语句中进行
using(Program3 p3 = new Program3())
{
//do your job
} // here the p3.Dispose gets called
重要的是要注意 Dispose 的要点是释放非托管资源。你得到的东西。Net 已经被管理,所以只有当你正在实现你自己的东西时,你才应该使用 IDisposable。
我只是像这样在 class 中继承 IDisposable 接口。
public class Program3:IDisposable
{
}
上面class创建实例时,我想手动销毁对象还是自动销毁对象?
注意:我没有明确使用 Dispose 方法来处置对象
您必须实现 IDisposable 接口方法:
public void Dispose()
{
// Clear all unmanaged resources
}
无论何时实例化对象,都应该在 using 语句中进行
using(Program3 p3 = new Program3())
{
//do your job
} // here the p3.Dispose gets called
重要的是要注意 Dispose 的要点是释放非托管资源。你得到的东西。Net 已经被管理,所以只有当你正在实现你自己的东西时,你才应该使用 IDisposable。