title: .NET object events and dispose / GC
body: <p><strong>EDIT:</strong> After Joel Coehoorns excellent answer, I understand that I need to be more specific, so I modified my code to be closer to thing I'm trying to understand...</p>
  
  <p><strong>Events:</strong> As I understand, in the background the events are "collection" of EventHandlers aka Delegates which will be executed when event raised. So for me it means that if object <strong>Y</strong> has event E and object X subscribes to event <strong>Y.E</strong>, then <strong>Y</strong> will have reference to X, since <strong>Y</strong> must execute the method located in X, in that way, <strong>X can not be collected</strong>, and that thing i understand.</p>
  
  <pre><code>//Creates reference to this (b) in a.
  a.EventHappened += new EventHandler(this.HandleEvent);
  </code></pre>
  
  <p><strong>But it is not what Joel Coehoorn tells...</strong></p>
  
  <blockquote>
    <p>However, there is an issue with events such that sometimes people like to use IDisposable with types that have events. The problem is that when a type X subscribes to events in another type Y, X now has a reference to Y. This reference will prevent Y from being collected.</p>
  </blockquote>
  
  <p>I not understand how <strong>X</strong> will reference the Y ???</p>
  
  <p>I modified a bit my example to illustrate my case more closer:</p>
  
  <pre><code>class Service //Let's say it's windows service that must be 24/7 online
  {       
      A _a;
  
      void Start()
      {
         CustomNotificationSystem.OnEventRaised += new EventHandler(CustomNotificationSystemHandler)
         _a = new A();
  
         B b1 = new B(_a);
         B b2 = new B(_a);
         C c1 = new C(_a);
         C c2 = new C(_a);
      }
  
      void CustomNotificationSystemHandler(args)
      {
  
          //_a.Dispose(); ADDED BY **EDIT 2***
          a.Dispose();
  
          _a = new A();
          /*
          b1,b2,c1,c2 will continue to exists as is, and I know they will now subscribed
          to previous instance of _a, and it's OK by me, BUT in that example, now, nobody
          references the previous instance of _a (b not holds reference to _a) and by my
          theory, previous instance of _a, now may be collected...or I'm missing
          something???
          */
      }
  
  }  
  
  class A : IDisposable
          {
             public event EventHandler EventHappened;
          }
  
          class B
          {          
             public B(A a) //Class B does not stores reference to a internally.
             {
                a.EventHappened += new EventHandler(this.HandleEventB);
             }
  
             public void HandleEventB(object sender, EventArgs args)
             {
             }
          }
  
          class C
          {          
             public C(A a) //Class B not stores reference to a internally.
             {
                a.EventHappened += new EventHandler(this.HandleEventC);
             }
  
             public void HandleEventC(object sender, EventArgs args)
             {
             }
          }
  </code></pre>
  
  <p><strong>EDIT 2:</strong> OK, now it's clear, when subscriber subscribes to a publishers events, it's <strong>NOT</strong> creates a reference to the publisher in subscriber. Only the reference from publisher to subscriber created (through EventHandler)...in this case when publisher collected by GC before the subscriber (subscribers lifetime is greater then publishers), there's no problem. </p>
  
  <p><strong>BUT</strong>...as I know, it's not guaranteed when GC will collect the publisher so in theory, even if subscribers lifetime is greater then publishers, it can happen that subscriber is legal for collection, but publisher is still not collected (I don't know if within closest GC cycle, GC will be smart enough to collect publisher first and then subscriber.</p>
  
  <p>Anyway, in such case, since my subscriber do not have direct reference to publisher and can't unsubscribe the event, I would like to make publisher to implement IDisposable, in order to dispose it before delete all references to him (see in CustomNotificationSystemHandler in my example).</p>
  
  <p><strong>AND AGAIN</strong> What I should write in publishers dispose method in order to clear all references to subscribers? should it be EventHappened -= null; or EventHappened = null; or there's no way to do it in such way, and I need to make something like below ???</p>
  
  <pre><code>public event EventHandler EventHappened
     {
        add 
        {
           eventTable["EventHappened"] = (EventHandler)eventTable["EventHappened"] + value;
        }
        remove
        {
           eventTable["EventHappened"] = (EventHandler)eventTable["EventHappened"] - value; 
        }
     }
  </code></pre>