Skip to content

Instantly share code, notes, and snippets.

@jasonporritt
Created October 27, 2011 13:58
Show Gist options
  • Save jasonporritt/1319609 to your computer and use it in GitHub Desktop.
Save jasonporritt/1319609 to your computer and use it in GitHub Desktop.
Memoization in C#: One Input
public static Func<TSource, TReturn> Memoize<TSource, TReturn>(Func<TSource, TReturn> func)
{
var cache = new Dictionary<TSource, TReturn>();
return s =>
{
if (!cache.ContainsKey(s))
{
cache[s] = func(s);
}
return cache[s];
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment