I was getting really bored with testing .ContainsKey() at each time I wanted to read a value from a dictionary.
Dictionary<string, string> dico; if (dico.ContainsKey("key")) value = dico["key"]; else value = "default";
public static class MyExtensions { public static TValue GetValue<TKey, TValue>( this IDictionary<TKey, TValue> source, TKey key, TValue defaultValue) { if (source.ContainsKey(key)) return source[key]; else return defaultValue; } }
value = dico.GetValue("key", "default");