CA1001: Types that own disposable fields should be disposable
Non-breaking - If the type is not visible outside the assembly.
Breaking - If the type is visible outside the assembly.
A class implements the IDisposable interface to dispose of unmanaged resources that it owns. An instance field that is an IDisposable type indicates that the field owns an unmanaged resource. A class that declares an IDisposable field indirectly owns an unmanaged resource and should implement the IDisposable interface. If the class does not directly own any unmanaged resources, it should not implement a finalizer.
The following example shows a class that violates the rule and a class that satisfies the rule by implementing IDisposable. The class does not implement a finalizer because the class does not directly own any unmanaged resources.
Imports System Imports System.IO Namespace DesignLibrary ' This class violates the rule. Public Class NoDisposeMethod Dim newFile As FileStream Sub New() newFile = New FileStream("c:\temp.txt", FileMode.Open) End Sub End Class ' This class satisfies the rule. Public Class HasDisposeMethod Implements IDisposable Dim newFile As FileStream Sub New() newFile = New FileStream("c:\temp.txt", FileMode.Open) End Sub Overloads Protected Overridable Sub Dispose(disposing As Boolean) If disposing Then ' dispose managed resources newFile.Close() End If ' free native resources End Sub 'Dispose Overloads Public Sub Dispose() Implements IDisposable.Dispose Dispose(True) GC.SuppressFinalize(Me) End Sub 'Dispose End Class End Namespace