Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Usage Warnings
 Finalizers should call base class f...
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual Studio Team System
Finalizers should call base class finalizer

TypeName

FinalizersShouldCallBaseClassFinalizer

CheckId

CA2220

Category

Microsoft.Usage

Breaking Change

NonBreaking

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

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.

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

Do not exclude 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.

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

Visual Basic
Imports System

Namespace UsageLibrary

    Public Class TypeB
        Inherits TypeA

        Friend Overrides Sub Dispose(ByVal disposing As Boolean)
            Try
                Dispose(False)
            Finally
                MyBase.Finalize()
            End Try
        End Sub 'Dispose
    End Class 'TypeB

End Namespace
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Sample is wrong      David M. Kean - MSFT   |   Edit   |  

The above sample is incorrect, it actually shows the Dispose method rather than the Finalize.

The following is a better example:

[Visual Basic]
 
Public Class TypeB
    Inherits TypeA
 
    Protected Overrides Sub Finalize()
        Try
         Dispose(False)
        Finally
        MyBase.Finalize()
     End Try
End Sub
 
End Class

 

No sample is needed for C#, because, as mentioned above, the C# compiler automatically calls the base class's finalizer, therefore you do not need to do anything.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker