The indexer into Dictionary
throws an exception if the key is missing. Is there an implementation of IDictionary
that instead will return de
It could be a one-liner to check TryGetValue
and return default value if it is false
.
Dictionary myDic = new Dictionary() { { "One", 1 }, { "Four", 4} };
string myKey = "One"
int value = myDic.TryGetValue(myKey, out value) ? value : 100;
myKey = "One"
=> value = 1
myKey = "two"
=> value = 100
myKey = "Four"
=> value = 4
Try it online