title: Why are lambda expressions not "interned"?
body: <p>Strings are reference types, but they are immutable. This allows for them to be <em>interned</em> by the compiler; everywhere the same string literal appears, the same object may be referenced.</p>
  
  <p>Delegates are also immutable reference types. (Adding a method to a multicast delegate using the <code>+=</code> operator constitutes <em>assignment</em>; that's not mutability.) And, like, strings, there is a "literal" way to represent a delegate in code, using a lambda expression, e.g.:</p>
  
  <pre><code>Func&lt;int&gt; func = () =&gt; 5;
  </code></pre>
  
  <p>The right-hand side of that statement is an expression whose type is <code>Func&lt;int&gt;</code>; but nowhere am I explicitly invoking the <code>Func&lt;int&gt;</code> constructor (nor is an implicit conversion happening). So I view this as essentially a <em>literal</em>. Am I mistaken about my definition of "literal" here?</p>
  
  <p>Regardless, here's my question. If I have two variables for, say, the <code>Func&lt;int&gt;</code> type, and I assign identical lambda expressions to both:</p>
  
  <pre><code>Func&lt;int&gt; x = () =&gt; 5;
  Func&lt;int&gt; y = () =&gt; 5;
  </code></pre>
  
  <p>...what's preventing the compiler from treating these as the same <code>Func&lt;int&gt;</code> object?</p>
  
  <p>I ask because section 6.5.1 of the <a href="http://www.microsoft.com/downloads/en/details.aspx?familyid=DFBF523C-F98C-4804-AFBD-459E846B268E" rel="noreferrer">C# 4.0 language specification</a> clearly states:</p>
  
  <blockquote>
    <p>Conversions of semantically identical
    anonymous functions with the same
    (possibly empty) set of captured outer
    variable instances to the same
    delegate types are permitted (but not
    required) to return the same delegate
    instance. The term semantically
    identical is used here to mean that
    execution of the anonymous functions
    will, in all cases, produce the same
    effects given the same arguments.</p>
  </blockquote>
  
  <p>This surprised me when I read it; if this behavior is explicitly <em>allowed</em>, I would have expected for it to be implemented. But it appears not to be. This has in fact gotten a lot of developers into trouble, esp. when lambda expressions have been used to attach event handlers successfully without being able to remove them. For example:</p>
  
  <pre><code>class EventSender
  {
      public event EventHandler Event;
      public void Send()
      {
          EventHandler handler = this.Event;
          if (handler != null) { handler(this, EventArgs.Empty); }
      }
  }
  
  class Program
  {
      static string _message = "Hello, world!";
  
      static void Main()
      {
          var sender = new EventSender();
          sender.Event += (obj, args) =&gt; Console.WriteLine(_message);
          sender.Send();
  
          // Unless I'm mistaken, this lambda expression is semantically identical
          // to the one above. However, the handler is not removed, indicating
          // that a different delegate instance is constructed.
          sender.Event -= (obj, args) =&gt; Console.WriteLine(_message);
  
          // This prints "Hello, world!" again.
          sender.Send();
      }
  }
  </code></pre>
  
  <p><strong>Is there any reason why this behavior—one delegate instance for semantically identical anonymous methods—is not implemented?</strong></p>