CA2215: Dispose methods should call base class dispose

Property Value
Rule ID CA2215
Title Dispose methods should call base class dispose
Category Usage
Fix is breaking or non-breaking Non-breaking
Enabled by default in .NET 8 No

Cause

A type that implements System.IDisposable inherits from a type that also implements IDisposable. The Dispose method of the inheriting type does not call the Dispose method of the parent type.

Rule description

If a type inherits from a disposable type, it must call the Dispose method of the base type from within its own Dispose method. Calling the base type Dispose method ensures that any resources created by the base type are released.

How to fix violations

To fix a violation of this rule, call base.Dispose in your Dispose method.

When to suppress warnings

It is safe to suppress a warning from this rule if the call to base.Dispose occurs at a deeper calling level than the rule checks.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable CA2215
// The code that's violating the rule is on this line.
#pragma warning restore CA2215

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.CA2215.severity = none

For more information, see How to suppress code analysis warnings.

Example

The following example shows two types, TypeA that implements IDisposable, and TypeB that inherits from type TypeA and correctly calls its Dispose method.

Namespace ca2215

    Public Class TypeA
        Implements IDisposable

        Protected Overridable Overloads Sub Dispose(disposing As Boolean)
            If disposing Then
                ' dispose managed resources
            End If
            
            ' free native resources
        End Sub

        Public Overloads Sub Dispose() Implements IDisposable.Dispose
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub

        ' Disposable types implement a finalizer.
        Protected Overrides Sub Finalize()
            Dispose(False)
            MyBase.Finalize()
        End Sub

    End Class

    Public Class TypeB
        Inherits TypeA

        Protected Overrides Sub Dispose(disposing As Boolean)
            If Not disposing Then
                MyBase.Dispose(False)
            End If
        End Sub

    End Class

End Namespace
using System;

namespace ca2215
{
    public class TypeA : IDisposable
    {
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // Dispose managed resources
            }

            // Free native resources
        }

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

        // Disposable types implement a finalizer.
        ~TypeA()
        {
            Dispose(false);
        }
    }

    public class TypeB : TypeA
    {
        protected override void Dispose(bool disposing)
        {
            if (!disposing)
            {
                base.Dispose(false);
            }
        }
    }
}

See also