Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this possible somehow in C# : if (a==b==c==d) {...}

Is there a quick way to compare equality of more than one values in C#?

something like:

if (5==6==2==2){

//do something

}

Thanks

like image 521
pencilCake Avatar asked Apr 30 '10 09:04

pencilCake


2 Answers

if (a == b && b == c && c == d) {
    // do something
}
like image 182
Joachim Sauer Avatar answered Oct 20 '22 18:10

Joachim Sauer


In C#, an equality operator (==) evaluates to a bool so 5 == 6 evaluates to false.

The comparison 5 == 6 == 2 == 2 would translate to

(((5 == 6) == 2) == 2)

which evaluates to

((false == 2) == 2)

which would try to compare a boolwith an int. Only if you would compare boolean values this way would the syntax be valid, but probably not do what you want.

The way to do multiple comparison is what @Joachim Sauer suggested:

 a == b && b == c && c == d
like image 45
Zano Avatar answered Oct 20 '22 17:10

Zano