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
Here is the "ultimate" solution, in that it is implemented as an extension method, uses the IDictionary interface, provides an optional default value, and is written concisely.
public static TV GetValueOrDefault(this IDictionary dic, TK key,
TV defaultVal=default(TV))
{
TV val;
return dic.TryGetValue(key, out val)
? val
: defaultVal;
}