Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary finalizers #49

Closed
alexandrnikitin opened this issue Feb 14, 2014 · 1 comment
Closed

Remove unnecessary finalizers #49

alexandrnikitin opened this issue Feb 14, 2014 · 1 comment
Assignees

Comments

@alexandrnikitin
Copy link

You shouldn't declare finalizers unless you have to. You should only declare a finalizer if you have unmanaged resources to dispose. The CLR deals with finalizable objects differently, even if SuppressFinalize is called. More info on msdn

public class Bus : IBus, IDisposable
{
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    // No need in finalizer
    ~Bus()
    {
        Dispose(false);
    }

    protected virtual void Dispose(bool disposing)
    {
        // Because no unmanaged resources here
        if (!disposing) return;
        //...
    }
}
@uglybugger
Copy link
Contributor

Thanks for the tip. I did a bunch more reading and it would appear that you're right - the recommended best practice is now to only use a finalizer if the class itself has unmanaged resources. Looks like I'm showing my age.

The "Basic IDisposable Pattern" here is different to the one here. Both from MSDN. I thought that CA1063 required a finalizer but that appears to not be the case, either.

I learnt a thing today. Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants