Created
September 23, 2014 01:46
-
-
Save wilson0x4d/ceafddd9864654f1aad2 to your computer and use it in GitHub Desktop.
// t(o.Ot)
This file contains hidden or 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 DictionaryExtensions | |
{ | |
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) | |
{ | |
return dictionary.GetValueOrDefault(key, x=>default(TValue)); | |
} | |
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, | |
Func<TKey, TValue> func) | |
{ | |
TValue result; | |
return dictionary.TryGetValue(key, out result) ? result : func(key); | |
} | |
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) | |
{ | |
TValue result; | |
return dictionary.TryGetValue(key, out result) ? result : value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://stackoverflow.com/questions/2601477/dictionary-returning-a-default-value-if-the-key-does-not-exist