title: What best practices for cleaning up event handler references?
body: <p>Often I find myself writing code like this:</p>
  
  <pre><code>        if (Session != null)
          {
              Session.KillAllProcesses();
              Session.AllUnitsReady -= Session_AllUnitsReady;
              Session.AllUnitsResultsPublished -= Session_AllUnitsResultsPublished;
              Session.UnitFailed -= Session_UnitFailed;
              Session.SomeUnitsFailed -= Session_SomeUnitsFailed;
              Session.UnitCheckedIn -= Session_UnitCheckedIn;
              UnattachListeners();
          }
  </code></pre>
  
  <p>The purpose being to clean up all event subscriptions that we have registered for on the target (Session) so that Session is free to be disposed by the GC.  I had a discussion with a co-worker about classes that implement IDisposable however and it was his belief that those classes should preform cleanup like this:</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Disposes the object
      /// &lt;/summary&gt;
      public void Dispose()
      {
          SubmitRequested = null; //frees all references to the SubmitRequested Event
      }
  </code></pre>
  
  <p>Is there a reason for prefering one over the other? Is there a better way to go about this altogether? (Aside from weak reference events everywhere)</p>
  
  <p>What I'd really like to see is somethign akin to the safe invocation pattern for raising events:  i.e. safe and repeatable.  Something I can remember to do everytime I attach to an event so that I can ensure it will be easy for me to clean  up.</p>