Skip to content

Instantly share code, notes, and snippets.

@jasonporritt
Created October 27, 2011 13:59
Show Gist options
  • Save jasonporritt/1319611 to your computer and use it in GitHub Desktop.
Save jasonporritt/1319611 to your computer and use it in GitHub Desktop.
Memoization in C#: Complex Input
public static Func<TSource1, TSource2, TReturn> Memoize<TSource1, TSource2, TReturn>(Func<TSource1, TSource2, TReturn> func)
{
var cache = new Dictionary<string, TReturn>();
return (s1, s2) =>
{
var key = s1.GetHashCode().ToString() + s2.GetHashCode().ToString();
if (!cache.ContainsKey(key))
{
cache[key] = func(s1, s2);
}
return cache[key];
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment