All rules
CA1001Design Enabled by default: No

Types that own disposable fields should be disposable

Types that own disposable fields should be disposable

Microsoft docs

Description

A class that declares an System.IDisposable field indirectly owns an unmanaged resource. The class should implement the System.IDisposable interface to dispose of the unmanaged resource that it owns once the resource is no longer in use. If the class does not *directly* own any unmanaged resources, it should not implement a finalizer.

This rule respects types implementing System.IAsyncDisposable as disposable types.

Cause

A class declares and implements an instance field that is an System.IDisposable type, and the class does not implement System.IDisposable.

By default, this rule analyzes the entire codebase, but this is configurable.

How to fix violations

To fix a violation of this rule, implement the System.IDisposable interface. In the System.IDisposable.Dispose method, call the System.IDisposable.Dispose method of the field's type.

Example

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

When to suppress

In general, do not suppress a warning from this rule. It's okay to suppress the warning when the dispose ownership of the field is not held by the containing type.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0