-5

It doesn't work:-

if(keyval.Value == "menu") // false

but this works:-

if(keyval.Value.ToString() == "menu") // true

what is the difference between these two.

why the first condition isn't working?

Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
Appu
  • 25
  • 7

1 Answers1

5

Presumably, keyval.Value has a type of object. That means that instead of comparing the two strings by value, you are comparing them by reference - and it's perfectly valid for two strings with the same value to be a different instance.

The proper way to do the comparison would be

if((string)keyval.Value == "menu") 

if the values are always strings. Or just avoid storing strings as objects :)

Luaan
  • 62,244
  • 7
  • 97
  • 116