title: When is it possible to call Finalize in Dispose?
body: <p>I was browsing the decompiled source code for a DLL in Reflector, and I came across this C# code:</p>
  
  <pre><code>protected virtual void Dispose([MarshalAs(UnmanagedType.U1)] bool flag1)
  {
      if (flag1)
      {
          this.~ClassName();
      }
      else
      {
          base.Finalize();
      }
  }
  </code></pre>
  
  <p>My first reaction was "What? I thought you couldn't call the finalizer manually!"</p>
  
  <p>Note: The base type is <code>object</code>.</p>
  
  <p>To be sure it wasn't a Reflector quirk, I opened up the method in ILSpy. It generated similar code.</p>
  
  <p>I went to Google to confirm my new discovery. I found the documentation for <a href="http://msdn.microsoft.com/en-us/library/system.object.finalize.aspx"><code>Object.Finalize</code></a>, and this is what it said:</p>
  
  <blockquote>
    <p>Every implementation of Finalize in a derived type must call its base type's implementation of Finalize. <strong>This is the only case in which application code is allowed to call Finalize.</strong></p>
  </blockquote>
  
  <p>Now I don't what to think. It might be because the DLL was compiled with C++. (Note: I couldn't find the implementation of Dispose. Maybe it's auto-generated.) It might be a special allowance for the <code>IDisposable.Dispose</code> method. It might be a flaw in both decompilers.</p>
  
  <p>Some observations:</p>
  
  <ul>
  <li>I couldn't find the implementation of Dispose in the source code. Maybe it's auto-generated.</li>
  <li>Reflector shows a method named <code>~ClassName</code>. It seems as though this method might not actually be the finalizer, but the C++ destructor, or even an ordinary method.</li>
  </ul>
  
  <p>Is this legal C#? If so, what is different about this case? If not, what is actually happening? Is it allowed in C++/CLI, but not C#? Or is it just a glitch in the decompiler?</p>