title: How to implement IDisposable interface in a class inherited from SocketAsyncEventArgs
body: <p>I work on a huge project in C# .NET 4.0. There is a custom class inherited from <a href="https://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs(v=vs.100).aspx" rel="noreferrer">System.Net.Sockets.<strong>SocketAsyncEventArgs</strong></a> class. Something like the following:</p>
  
  <pre><code>public class SocketTaskArgs : SocketAsyncEventArgs
  {
      public SocketTaskArgs()
      {
          Completed += someEventhHandler;
      }
  
      public void CleanUp()
      {
          Completed -= someEventhHandler;
      }
  
      /* 
          There is a lot of code here that is unimportant at the moment.
      */
  }
  </code></pre>
  
  <p>So, I wanted to move the content of CleanUp() method to Dispose(bool) method. </p>
  
  <p>As first, I checked the source code of the base class - SocketAsyncEventArgs (using <strong>Go To Definition</strong> so that I saw metadata as source). I found out, this class implements IDisposable interface. Nice, I just need to override the Dispose(bool) method, don't I? (See <a href="https://msdn.microsoft.com/en-us/library/system.idisposable.aspx" rel="noreferrer">IDisposable Interface on MSDN</a>, the "<em>IDisposable and the inheritance hierarchy</em>" section, for more details). Nothing new for me... Unfortunately, the SocketAsyncEventArgs class is implemented as following:</p>
  
  <pre><code>public class SocketAsyncEventArgs : EventArgs, IDisposable
  {
      public void Dispose();
  
      //some other stuff here
  }
  </code></pre>
  
  <p>That means, there is no way how to override <em>Dispose(bool)</em> method, as it's implemented as <em>private</em> instead of <em>protected</em>... <strong>What is the reason for this?</strong></p>
  
  <p>Next, I read about <a href="https://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.dispose(v=vs.100).aspx" rel="noreferrer">SocketAsyncEventArgs.Dispose()</a> method on MSDN. The funny thing is that, it contains the following section:</p>
  
  <blockquote>
    <p><strong>Notes to Inheritors</strong> </p>
    
    <p><strong>Dispose</strong> can be called multiple times by other
    objects. When overriding <strong>Dispose(Boolean)</strong>, be careful not to reference
    objects that have been previously disposed of in an earlier call to
    <strong>Dispose</strong>. For more information about how to implement <strong>Dispose(Boolean)</strong>,
    see Implementing a Dispose Method.</p>
  </blockquote>
  
  <p>Wait... what?</p>
  
  <blockquote>
    <p>When overriding <strong>Dispose(Boolean)</strong>, ...</p>
  </blockquote>
  
  <p><strong>How am I supposed to override Dispose(Boolean)?</strong></p>
  
  <p><strong>What is the recommended way to implement IDisposable interface in this case?</strong></p>