本文實例講述了C#使用Dispose模式實現手動對資源的釋放。分享給大家供大家參考。具體實現方法如下:
//單一類的實現class MyClass : IDisposable { public MyClass(){} ~MyClass() { // In case the client forgets to call // Dispose , destructor will be invoked for Dispose(false); } protected virtual void Dispose(bool disposing) { if (disposing) { // Free managed objects. } // Free unmanaged objects } public void Dispose() { Dispose(true); // Ensure that the destructor is not called GC.SuppressFinalize(this); } }
希望本文所述對大家的C#程序設計有所幫助。
新聞熱點
疑難解答