Currently I\'m using
var x = dict.ContainsKey(key) ? dict[key] : defaultValue
I\'d like some way to have dictionary[key] return null for nonexi
With an extension method:
public static class MyHelper
{
public static V GetValueOrDefault(this IDictionary dic,
K key,
V defaultVal = default(V))
{
V ret;
bool found = dic.TryGetValue(key, out ret);
if (found) { return ret; }
return defaultVal;
}
void Example()
{
var dict = new Dictionary();
dict.GetValueOrDefault(42, "default");
}
}