title: Dispose vs Dispose(bool)
body: <p>I am confused about dispose.  I am trying to get my code disposing resources correctly. So I have been setting up my classes as <code>IDisposable</code> (with a <code>Dispose</code> method) them making sure that the <code>Dispose</code> method gets called.</p>
  <p>But now FXCop is telling me lots of stuff about <code>disposing = false</code> and calling <code>Dispose(false)</code>.</p>
  <p>I don't see a <code>Dispose</code> method that takes a bool.  Do I need to make one?  If so, why?  Why not just have a method that gets called when it is disposing?</p>
  <p>I saw some code here: <a href="https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1063-implement-idisposable-correctly" rel="noreferrer">CA1063: Implement IDisposable correctly - Microsoft Docs</a> that shows how to make a <code>Dispose</code> method that takes a <code>bool</code>. It says it is for native vs managed resourses. <strong>But I thought the whole point of dispose was for unmanaged resourses only.</strong></p>
  <p>Also, the line that FXCop is complaining about is this:</p>
  <pre><code>~OwnerDrawnPanel()
  {
      _font.Dispose();
  }
  </code></pre>
  <p>It says:</p>
  <blockquote>
  <p>CA1063 : Microsoft.Design : Modify 'OwnerDrawnPanel.~OwnerDrawnPanel()' so that it calls <strong>Dispose(false)</strong> and then returns.</p>
  </blockquote>
  <p>But <code>Font</code> does not have a <code>Dispose(bool)</code> on it (that I can find).</p>
  <h2>To sum it up:</h2>
  <p>Why do I need a <code>Dispose(bool)</code>? and if I do, why doesn't Font have it? and since it does not have it, why is FXCop asking me to use it?</p>
  <hr />
  <p>Thanks for all the great answers.  I think I understand now.  Here is</p>
  <h2>The answer as I see it:</h2>
  <p>Disposing of &quot;unmanaged&quot; resources falls into two categories:</p>
  <ol>
  <li>Resources that are wrapped in a managed class (ie <code>Bitmap</code>, <code>Font</code> etc), but still need <code>Dispose</code> to be called to clean them up properly.</li>
  <li>Resources that you have allocated, which are representations of native resources (ie device contexts that need to be released)</li>
  </ol>
  <p><code>Dispose(bool)</code> is used to tell the difference between the two:</p>
  <ol>
  <li>When <code>Dispose</code> is directly called on your object, you want to free both kinds of &quot;unmanaged&quot; resources.</li>
  <li>When your object is up for Garbage Collection, you don't need to worry about the first kind of resources. The garbage collector will take care of them when it cleans them up.  You only need to worry about true native resources that you have allocated (if any).</li>
  </ol>