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.
CA2015: Do not define finalizers for types derived from MemoryManager
| Property | Value |
|---|---|
| Rule ID | CA2015 |
| Title | Do not define finalizers for types derived from MemoryManager |
| Category | Reliability |
| Fix is breaking or non-breaking | Non-breaking |
| Enabled by default in .NET 10 | As warning |
| Applicable languages | C# and Visual Basic |
Cause
Defining finalizers for types derived from MemoryManager
Rule description
Adding a finalizer to a type derived from MemoryManager
Note
The MemoryManager
How to fix violations
To fix the violation, remove the finalizer definition.
class DerivedClass : MemoryManager
{
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.