Fork me on GitHub
TypeName DisposableFieldNotDisposedAnalyzer
Check Id CC0032
Category Usage
Severity Info

Cause

A type has a disposable field (a field that implements IDisposable). This field is being assigned on the type construction through a method call. It may be possible that the enclosing type owns the object and should then dispose it as well, implementing IDisposible itself (if it does not yet implement yet).

Example

Assuming class D implements IDisposable:

class TypeName
{
    private D field = D.Create();
}

Code fix

A code fix will be presented to you that will transform the code, it will implement IDisposable if the type has not implemented it yet, and add a call do Dispose on the field.

class TypeName : System.IDisposable
{
    private D field = D.Create();

    public void Dispose()
    {
        field.Dispose();
    }
}

Code fix

Related rules

  • CC0022 - Disposable variable not disposed
  • CC0029 - Disposable should call SuppressFinalize
  • CC0033 - same rule, but field is being assigned using object contruction

See also

TBD