title: C# caching with delegates
body: <p>I'm working on a simple cache class for my application.</p>
  
  <pre><code>using System;
  namespace Program {
    public class Cache {
      public delegate int CacheMethodInt();
      public static int Get(CacheMethodInt method) {
        //todo: generate cachekey here
        return method.Invoke();
      }
    }
  
    public class Calculator {
      public int Add(int x, int y) {
        return x + y;
      }
    }
  
    class Program {
      static void Main(string[] args) {
        Calculator c = new Calculator();
        Console.WriteLine(Cache.Get(() =&gt; c.Add(1, 2)));
      }
    }
  }
  </code></pre>
  
  <p>In <code>Cache:Get</code> I need to check whether the return value is already cached and if so return it without invoking the method. The problem is that I can't figure out how to generate a good cachekey. In this case I would like something like this:</p>
  
  <pre><code>Calculator:Add:1(int):2(int)
  </code></pre>
  
  <p>Is it possible to get this info in <code>Cache:Get</code>? I'm using .NET 2.0.</p>