title: Implementing IDisposable on a sealed class
body: <p>I don't think this question has been asked before. I'm a bit confused on the best way to implement <code>IDisposable</code> on a sealed class—specifically, a sealed class that does not inherit from a base class. (That is, a "pure sealed class" which is my made up term.)</p>
  
  <p>Perhaps some of you agree with me in that the guidelines for implementing <code>IDisposable</code> are very confusing. That said, I want to know that the way I intend to implement <code>IDisposable</code> is sufficient and safe.</p>
  
  <p>I'm doing some P/Invoke code that allocates an <code>IntPtr</code> through <code>Marshal.AllocHGlobal</code> and naturally, I want to cleanly dispose of the unmanaged memory I've created. So I'm thinking of something like this</p>
  
  <pre><code>using System.Runtime.InteropServices;
  
  [StructLayout(LayoutKind.Sequential)]
  public sealed class MemBlock : IDisposable
  {
       IntPtr ptr;
       int length;
  
       MemBlock(int size)
       {
             ptr = Marshal.AllocHGlobal(size);
             length = size;
       }
  
       public void Dispose()
       {
            if (ptr != IntPtr.Zero)
            {
                 Marshal.FreeHGlobal(ptr);
                 ptr = IntPtr.Zero;
                 GC.SuppressFinalize(this);
            }
       }
  
       ~MemBlock()
       {
             Dispose();
       }    
  }
  </code></pre>
  
  <p>I'm assuming that because <code>MemBlock</code> is completely sealed and never derives from another class that implementing a <code>virtual protected Dispose(bool disposing)</code> is not necessary.  </p>
  
  <p>Also, is the finalizer strictly necessary? All thoughts welcome.</p>