本文介绍了C#中的析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Finalize()是C#中的析构函数,并且由GC使用.
Finalize() is a destructor in C# and it is used by the GC.
Is there anyway we can use it when we want to free the memory space?
推荐答案
public class YourClass : IDisposable
{
private bool isDisposed;
~YourClass()
{
Dispose(false);
}
public bool IsDisposed
{
get{ return isDisposed; }
}
public void Dispose()
{
Dispose(true);
GC.SupressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if(!isDisposed)
{
if(disposing)
{
// dispose managed resources here
}
// dispose unmanaged resources here
isDisposed = true;
}
}
}
这篇关于C#中的析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!