Skip to content

Instantly share code, notes, and snippets.

@jfromaniello
Created July 6, 2010 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfromaniello/464895 to your computer and use it in GitHub Desktop.
Save jfromaniello/464895 to your computer and use it in GitHub Desktop.
public static class DictionaryExtensions
{
public static TValue GetOrAdd<TKey, TValue>(
this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> create)
{
TValue result;
if(!dictionary.TryGetValue(key, out result))
{
result = create();
dictionary[key] = result;
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment