Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
| Property | Value | 
|---|---|
| Rule ID | CA2015 | 
| Title | Do not define finalizers for types derived from MemoryManager<T> | 
| Category | Reliability | 
| Fix is breaking or non-breaking | Non-breaking | 
| Enabled by default in .NET 9 | As warning | 
Cause
Defining finalizers for types derived from MemoryManager<T>
Rule description
Adding a finalizer to a type derived from MemoryManager<T> is likely an indication of a bug, as it suggests a native resource that could have been handed out in a Span<T> is getting cleaned up and potentially while it is still in use by the Span<T>.
Note
The MemoryManager<T> class is intended for advanced scenarios. Most developers do not need to use it.
How to fix violations
To fix the violation, remove the finalizer definition.
class DerivedClass <T> : MemoryManager<T>
{
    public override bool Dispose(bool disposing)
    {
        if (disposing)
        {
            _handle.Dispose();
        }
    }
    ...
    // Violation occurs, remove the finalizer to fix the warning.
    ~DerivedClass() => Dispose(false);
}
When to suppress warnings
It is safe to suppress a violation of this rule if the intent is to create a finalizer for debugging or validation purposes.
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 CA2015
// The code that's violating the rule is on this line.
#pragma warning restore CA2015
To disable the rule for a file, folder, or project, set its severity to none in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA2015.severity = none
To disable this entire category of rules, set the severity for the category to none in the configuration file.
[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Reliability.severity = none
For more information, see How to suppress code analysis warnings.