c# - How to check dictionary if string value exist for conditional comparison -


if want use dictionary given values conditional comparison in if-statement pass implementation avoid if-statement listing of values , dictionary. how check dictionary, contains 400 strings, if string value exist:

    dictionary<int, string> set1 = new dictionary<int, string>()                               {{ 1, "string1" }, { 2, "string2" } ... }; // 400 values 

so seems way wrong:

    string str = "string1";      if (set1.containskey(str) == true)     {          console.writeline("contains");     }     else     {          console.writeline("does not contains");     } 

to result further condition

containsvalue(value);

string str = "string1";  if (set1.containsvalue(str) == true) {      console.writeline("contains"); } else {      console.writeline("does not contains"); } 

or linq:

using system.linq; ... string str = "string1";  if (set1.values.any(x => x == str)) {      console.writeline("contains"); } else {      console.writeline("does not contains"); } 

Comments

Popular posts from this blog

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

arrays - Algorithm to find ideal starting spot in a circle -

php - Autoloader issue not returning Class -