title: When are anonymous delegates removed from events?
body: <p>If I write something like</p>
  
  <pre><code>myDataModel.ContentLoaded += (o,e) =&gt; { DoSomething(); }
  </code></pre>
  
  <p>When (if ever) is that anonymous delegate removed from the event?</p>
  
  <p>As a quick example, I could write this program</p>
  
  <pre><code>class Program
  {
      public event EventHandler&lt;EventArgs&gt; MyEvent = delegate { };
  
      static void Main(string[] args)
      {
          Program p = new Program();
          while(true)
          {
              p.MyEvent += (o, e) =&gt; Console.WriteLine("Hello!");
  
              Console.ReadLine();
              p.Foo();
          }
      }
  
      void Foo()
      {
          MyEvent(this, EventArgs.Empty);
      }
  }
  </code></pre>
  
  <p>And the output as I press 'Enter' repeatedly is</p>
  
  <pre><code>Hello!
  
  Hello!
  Hello!
  
  Hello!
  Hello!
  Hello!
  
  Hello!
  Hello!
  Hello!
  Hello!
  </code></pre>
  
  <p>...and so forth. I could add the line</p>
  
  <pre><code>p.MyEvent -= (o, e) =&gt; Console.WriteLine("Hello!");
  </code></pre>
  
  <p>after <code>p.Foo()</code> but of course it has no effect because I'm removing an entirely different anonymous delegate.</p>
  
  <p>So what's the deal with this? Is there any way of removing these anonymous delegates at all? What are the implications for, say, an asynchronous Silverlight application where I'm pulling data using expressions like</p>
  
  <pre><code>_myDataContext.Load(myQuery, loadOperation =&gt;
      {
          // do stuff with the data here
      }, null);
  </code></pre>
  
  <p>? I assume that these kinds of callbacks aren't implemented using events, but of course it's impossible(?) to tell.</p>
  
  <p>Are anonymous delegates dangerous if not carefully accounted for? </p>