Error: Do not override object.Finalize. Instead, provide a destructor

一笑奈何 提交于 2020-01-21 16:13:31

问题


Getting the above error in following code. How to rectify it. Thanks. Please look for

protected override void Finalize() {     Dispose(false); } 

in the below code.

using Microsoft.Win32; 
using System.Runtime.InteropServices; 

public class Kiosk : IDisposable 
{ 

    #region "IDisposable" 

    // Implementing IDisposable since it might be possible for 
    // someone to forget to cause the unhook to occur. I didn't really 
    // see any problems with this in testing, but since the SDK says 
    // you should do it, then here's a way to make sure it will happen. 

    public void Dispose() 
    { 
        Dispose(true); 
        GC.SuppressFinalize(this); 
    } 

    protected virtual void Dispose(bool disposing) 
    { 
        if (disposing) { 
        } 
        // Free other state (managed objects). 
        if (m_hookHandle != 0) { 
            UnhookWindowsHookEx(m_hookHandle); 
            m_hookHandle = 0; 
        } 
        if (m_taskManagerValue > -1) { 
            EnableTaskManager(); 
        } 
    } 

    protected override void Finalize() 
    { 
        Dispose(false); 
    } 

    #endregion 
} 

回答1:


Do what it says. Instead of:

protected override void Finalize() 
{ 
    Dispose(false); 
} 

Have:

~Kiosk () 
{ 
    Dispose(false); 
} 



回答2:


Finalize() is a special method that you can't override in code. Use the destructor syntax instead:

~Kiosk() 
{ 
    Dispose(false); 
} 



回答3:


In C#, the following syntax compiles to exactly what you're trying to accomplish.

~Kiosk()
{
    Dispose(false);
}


来源:https://stackoverflow.com/questions/1332658/error-do-not-override-object-finalize-instead-provide-a-destructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!