Condividi tramite


CA2220: Finalizers should call base class finalizer

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
RuleId CA2220
Category Microsoft.Usage
Breaking change Non-breaking

Cause

A type that overrides System.Object.Finalize does not call the Finalize method in its base class.

Note

This rule has been deprecated. For more information, see Deprecated rules.

Rule description

Finalization must be propagated through the inheritance hierarchy. To ensure this, types must call their base class Finalize method from within their own Finalize method. The C# compiler adds the call to the base class finalizer automatically.

How to fix violations

To fix a violation of this rule, call the base type's Finalize method from your Finalize method.

When to suppress warnings

Do not suppress a warning from this rule. Some compilers that target the common language runtime insert a call to the base type's finalizer into the Microsoft intermediate language (MSIL). If a warning from this rule is reported, your compiler does not insert the call, and you must add it to your code.

Example

The following Visual Basic example shows a type TypeB that correctly calls the Finalize method in its base class.

Imports System

Namespace UsageLibrary

  Public Class TypeB
      Inherits TypeA
   
      Protected Overrides Sub Finalize()
          Try
              Dispose(False)
          Finally
              MyBase.Finalize()
          End Try
      End Sub
   
  End Class
  
End Namespace

See also