Created
February 11, 2013 18:20
-
-
Save DTTerastar/4756366 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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