Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
54 lines (43 sloc) 2.92 KB
title ms.date ms.prod ms.technology ms.topic f1_keywords helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
CA1001: Types that own disposable fields should be disposable | Microsoft Docs
11/15/2016
visual-studio-dev14
vs-ide-code-analysis
reference
CA1001
TypesThatOwnDisposableFieldsShouldBeDisposable
CA1001
TypesThatOwnDisposableFieldsShouldBeDisposable
c85c126c-2b16-4505-940a-b5ddf873fb22
23
jillre
jillfra
wpickett

CA1001: Types that own disposable fields should be disposable

[!INCLUDEvs2017banner]

TypeName TypesThatOwnDisposableFieldsShouldBeDisposable
CheckId CA1001
Category Microsoft.Design
Breaking Change Non-breaking - If the type is not visible outside the assembly.

Breaking - If the type is visible outside the assembly.

Cause

A class declares and implements an instance field that is a xref:System.IDisposable?displayProperty=fullName type and the class does not implement xref:System.IDisposable.

Rule Description

A class implements the xref:System.IDisposable interface to dispose of unmanaged resources that it owns. An instance field that is an xref:System.IDisposable type indicates that the field owns an unmanaged resource. A class that declares an xref:System.IDisposable field indirectly owns an unmanaged resource and should implement the xref:System.IDisposable interface. If the class does not directly own any unmanaged resources, it should not implement a finalizer.

How to Fix Violations

To fix a violation of this rule, implement xref:System.IDisposable and from the xref:System.IDisposable.Dispose%2A?displayProperty=fullName method call the xref:System.IDisposable.Dispose%2A method of the field.

When to Suppress Warnings

Do not suppress a warning from this rule.

Example

The following example shows a class that violates the rule and a class that satisfies the rule by implementing xref:System.IDisposable. The class does not implement a finalizer because the class does not directly own any unmanaged resources.

[!code-csharpFxCop.Design.DisposableFields#1] [!code-vbFxCop.Design.DisposableFields#1]

Related Rules

CA2213: Disposable fields should be disposed

CA2216: Disposable types should declare finalizer

CA2215: Dispose methods should call base class dispose

CA1049: Types that own native resources should be disposable

You can’t perform that action at this time.