Equality comparison between multiple variables
Solution 1
KennyTM is correct, there is no other simpler or more efficient way.
However, if you have many variables, you could also build an array of the values and use the IEnumerable.All method to verify they're all 1. More readable, IMO.
if (new[] { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 }.All(x => x == 1))
Instead of
if(v1 == 1 && v2 == 1 && v3 == 1 && v4 == 1 && v5 == 1 && v6 == 1 && v7 == 1 && v8 == 1 && v9== 1 && v10 == 1)
Solution 2
if (x == y && y == z && z == 1)
is the best you can do, because
y == z
evaluates to a boolean and you can't compare x
with the result:
x == (y == z)
| |
int bool
I would do this:
public bool AllEqual<T>(params T[] values) {
if(values == null || values.Length == 0)
return true;
return values.All(v => v.Equals(values[0]));
}
// ...
if(AllEqual(x, y, z)) { ... }
Solution 3
If you just want to testif x == y == z you can use:
var allEqual = new[] {x, y, z}.Distinct().Count() == 1;
If you want to test if they're all equal to 1, add 1 to the set:
var allEqual1 = new[] {x, y, z, 1}.Distinct().Count() == 1;
or use All
as in fencliff's answer.
Solution 4
if (x == y && y == z && z == 1)
There are no other simple or more efficient ways.
Solution 5
For integral types, what about:
int x = 3, y = 3, z = 3;
if((x & y & z & 3) == 3)
//...have same data
for testing any non-zero value. It would need more checks to make this a re-usable function. But might work for inline checks of non-zero equality, as the OP described.
Related videos on Youtube
JPReddy
Passionate to give solutions to real world problems using various technologies suitable for the solution. Has hands on with technologies like Install Shield, VB6, C, C++, VC++, PHP, Microsoft technologies since Dot net framework 2.0 (C#, ASP.net, VB.net).
Updated on May 21, 2020Comments
-
JPReddy almost 4 years
I've a situation where I need to check whether multiple variables are having same data such as
var x=1; var y=1; var z=1;
I want to check whether x==1 and y==1 z==1 (it may be '1' or some other value). instead of this, is there any short way I can achieve same such as below
if(x==y==z==1)
Is this possible in C#?
-
JPReddy almost 14 yearsSorry to say that I'm not expecting this answer as even I know I can achieve it with such 3 expression check. My question might not be clear enough. I wanted to achieve this in single expression as I stated above without And operator. Anyway thanks for clarifying that there is no simpler way.
-
this. __curious_geek almost 14 years+1. brilliant. I'd accept this answer. It's not how much you know, it's all about how well you know whatever you already know. My answer looks silly in front of this.
-
Michael Anderson almost 12 yearsThing to watch out for in large sets this is O(N log N) while the accepted solution is O(N).
-
DeadlyChambers over 9 yearsSo for an OR (||) statement you could just do .Any instead of .All?
-
Żubrówka over 7 yearsJust adding on top of @MichaelAnderson, performance speaking, how will that Distinct() perform if you have an enough large set with different values?
-
Jazimov over 7 yearsIf you choose to use this in an if statement, surround the ^ (XOR) operators with parentheses as: if((x ^ y ^ z ^ 1)==0)
-
theonlygusti about 2 years@Jazimov why?.....
-
Piotr Golacki almost 2 years@theonlygusti otherwise you get a compiler error
-
Piotr Golacki almost 2 yearsthis does not really work as
x
andy
will be evaluated to a value (perhaps to0
if they're equal) and then this value will be XOR'ed withz