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

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -