title: UnHooking Events with Lambdas in C#
body: <p>I often run into a situation where I want to subscribe to an event, but I want to use a lambda to do so:</p>
  
  <pre><code>public class Observable
  {
      public event EventHandler SomethingHappened;
  
      public void DoSomething()
      {
          // Do Something...
          OnSomethingHappened();
      }
  }
  
  // Somewhere else, I hook the event
  observable.SomethingHappened += (sender, args) =&gt; Console.WriteLine("Something Happened");
  </code></pre>
  
  <p>The problem I run into is that I don't know how to unhook the event.  Since the lambda creates an anonymous delegate under the hood, I have nothing to call <code>-=</code> on.</p>
  
  <p>Now, I could just create a method:</p>
  
  <pre><code>private void SomethingHappened(object sender, EventArgs args)
  {
      Console.WriteLine("Something Happened");
  }
  </code></pre>
  
  <p>And then I can hook/unhook all I want:</p>
  
  <pre><code>observable.SomethingHappened += SomethingHappened;
  observable.SomethingHappened -= SomethingHappened;
  </code></pre>
  
  <p>But I'd really, really, really like to use my lambda instead.  In a more complicated example, lambdas come in really handy here.  </p>
  
  <p>I am pretty sure that I am out of luck... but I was wondering if anyone out there has figured out a way to do this?</p>