c# - Testing for Dictionary KeyNotFoundException -
i have scenario there dictionary might or might not have key value @ given time. presently testing see if value exists in following manner, know if best approach or if there better approach handling this.
int myint; try { myint = {value dictionary}; } catch { myint = 0; }
any input? thanks.
take @ dictionary's trygetvalue method
int myint; if (!_mydictionary.trygetvalue(key, out myint)) { myint = 0; }
a couple of people have suggested using containskey. not idea if want value because mean 2 lookups - e.g.
if (_mydictionary.containskey(key)) // 1 { myint = _mydictionary[key]; // 2 }
Comments
Post a Comment