title: C#: Is it possible to use expressions or functions as keys in a dictionary?
body: <p>Would it work to use <code>Expression&lt;Func&lt;T&gt;&gt;</code> or <code>Func&lt;T&gt;</code> as keys in a dictionary? For example to cache the result of heavy calculations.</p>
  
  <p>For example, changing my very basic cache from a <a href="https://stackoverflow.com/questions/1131305/c-how-to-implement-a-smart-cache">different question</a> of mine a bit:</p>
  
  <pre><code>public static class Cache&lt;T&gt;
  {
      // Alternatively using Expression&lt;Func&lt;T&gt;&gt; instead
      private static Dictionary&lt;Func&lt;T&gt;, T&gt; cache;
      static Cache()
      {
          cache = new Dictionary&lt;Func&lt;T&gt;, T&gt;();
      }
      public static T GetResult(Func&lt;T&gt; f)
      {
          if (cache.ContainsKey(f))
              return cache[f];
  
         return cache[f] = f();
      }
  }
  </code></pre>
  
  <p>Would this even work?</p>
  
  <p><strong><em>Edit:</strong> After a quick test, it seems like it actually works. But I discovered that it could probably be more generic, since it would now be one cache per return type... not sure how to change it so that wouldn't happen though... hmm</em></p>
  
  <p><strong><em>Edit 2:</strong> Noo, wait... it actually doesn't. Well, for regular methods it does. But not for lambdas. They get various random method names even if they look the same. Oh well c",)</em></p>