Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 3.0
Tools
Development Tools
FxCop
FxCop Warnings
Design Warnings
 Types that own disposable fields sh...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual Studio Team System
Types that own disposable fields should be disposable

TypeName

TypesThatOwnDisposableFieldsShouldBeDisposable

CheckId

CA1001

Category

Microsoft.Design

Breaking Change

NonBreaking, Breaking

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

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.

To fix a violation of this rule, implement IDisposable and from the System.IDisposable.Dispose method call the field's Dispose method.

Do not exclude a warning from this rule.

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

Visual Basic
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
C#
using System;
using System.IO;
  
namespace DesignLibrary
{
   // This class violates the rule.
   public class NoDisposeMethod
   {
      FileStream newFile;

      public NoDisposeMethod()
      {
         newFile = new FileStream(@"c:\temp.txt", FileMode.Open);
      }
   }

   // This class satisfies the rule.
   public class HasDisposeMethod: IDisposable
   {
      FileStream newFile;

      public HasDisposeMethod()
      {
         newFile = new FileStream(@"c:\temp.txt", FileMode.Open);
      }

      protected virtual void Dispose(bool disposing)
      {
         if (disposing)
            {
               // dispose managed resources
               newFile.Close();
            }
          // free native resources
      }

      public void Dispose()
      {
         Dispose(true);
         GC.SuppressFinalize(this);
      }
   }
}
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
The example above is incorrect      David M. Kean   |   Edit   |   Show History

The above example implements the Dispose method like the following:

[C#]
 
public void Dispose()
{
    Dispose(true);
    GC.SuppressFinalize(true);
}

 

[Visual Basic]
 
Overloads Public Sub Dispose() Implements IDisposable.Dispose
    Dispose(True)
    GC.SuppressFinalize(True)
End Sub

 

This is incorrect; it should actually read:

[C#]
 
public void Dispose()
{
    Dispose(true);
    GC.SuppressFinalize(this);
}
 

 

[Visual Basic]
 
Overloads Public Sub Dispose() Implements IDisposable.Dispose
    Dispose(True)
    GC.SuppressFinalize(Me)
End Sub
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker