Created
April 4, 2013 19:53
-
-
Save robfe/5313618 to your computer and use it in GitHub Desktop.
Dictionary GetOrCreate
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 class Extensions | |
{ | |
public static TValue GetOrCreate<TKey, TValue>(this Dictionary<TKey, TValue> d, TKey key, Func<TKey, TValue> valueFactory) | |
{ | |
TValue value; | |
if (!d.TryGetValue(key, out value)) | |
{ | |
d[key] = value = valueFactory(key); | |
} | |
return value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment