Skip to content

Instantly share code, notes, and snippets.

@DTTerastar
Created February 11, 2013 18:20
Show Gist options
  • Save DTTerastar/4756366 to your computer and use it in GitHub Desktop.
Save DTTerastar/4756366 to your computer and use it in GitHub Desktop.
public static TValue GetOrSet<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> create)
{
TValue value;
return dictionary.TryGetValue(key, out value) ? value : (dictionary[key] = create(key));
}
public static TValue GetOrSet<TKey, TValue>(this IDictionary dictionary, TKey key, Func<TKey, TValue> create)
{
var value = (TValue) dictionary[key];
if (!Equals(value, default(TValue))) return value;
var created = create(key);
dictionary[key] = created;
return created;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment