Skip to content

Instantly share code, notes, and snippets.

@wilson0x4d
Created September 23, 2014 01:46
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 wilson0x4d/ceafddd9864654f1aad2 to your computer and use it in GitHub Desktop.
Save wilson0x4d/ceafddd9864654f1aad2 to your computer and use it in GitHub Desktop.
// t(o.Ot)
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