Easy way to compare values ​​of more than three variables?

I want to check if these variables have the same values.

Example:

int a = 5; int b = 5; int c = 5; int d = 5; int e = 5; . . . int k = 5; if(a==b && b==c && c==d && d==e && .... && j==k) { //this is Complex way and not well understandable. } 

Any easy way to compare everything the same?
LIKE in the example below

 if(a==b==c==d==e.....j==k) { //Understandable but not work } 
+4
source share
12 answers

what about the following:

 if (Array.TrueForAll<int>(new int[] {a, b, c, d, e, f, g, h, i, j, k }, val => (a == val))) { // do something } 
+9
source

You can create a var args method for this:

 bool intsEqual (params int[] ints) { for (int i = 0; i < ints.Length - 1; i++) { if (ints[i] != ints[i+1]) { return False; } } return True; } 

Then just call it with all your goals as parameters:

 if (intsEqual(a, b, c, d, e, f, g, h, i, j, k)) { doSomeStuff(); } 
+4
source

With these many variables, would it be wise to move them to an array?

Then you can check if they are all equal using Linq expressions such as myarray.Distinct().Count() == 1; or possibly myarray.All(r => r == 5);

+4
source

Just a thought, but if you can calculate the standard deviation of the entire list and be zero, you will have your answer.

Here's the answer on the site that concerns this, which may help in this: Standard deviation of the general list?

An interesting problem. Good luck to you.

+2
source

I agree that the easiest way is to put them in a list, and then use the following for comparison. This is essentially a loop and a comparison with the first value, but it's a little cleaner.

 var match = counts.All(x => x == counts[0]) 
+2
source

What about

 int common = a; if (a==common && b==common && c==common && d==common && .... && k==common) 
+1
source

You can write a helper method as follows:

 public static bool AllEqual<T> (params T[] values) where T : IEquatable<T> { if (values.Length < 2) return true; for (int i = 1; i < values.Length; i++) if (!values[i].Equals (values[0])) return false; return true; } 

In special cases, this will be slightly different from the == operator:

 AllEqual (double.NaN, double.NaN).Dump(); // True (double.NaN == double.NaN).Dump(); // False 
+1
source

I know this is an old question that I came across, but I was wondering what happened to:

 if (a == (b & c & d & e & f & g & h & i & j & k)) { } 
+1
source

This does not work because a == b evaluates a boolean that cannot be compared with an integer, c. It seems that you have a better way.

0
source

You might want to put the values ​​in an array and use a for () loop. This is not entirely easier, but can help if the number of values ​​has changed.

0
source

You can use a helper function with variable arguments to make the comparison pretty easy.

 static bool CompareLongList(params int[] args) { if (args.Length > 1) { int value = args[0]; for (int i = 1; i < args.Length; ++i) { if (value != args[i]) return false; } } return true; } 

Then you just use the function as follows

 if(CompareLongList(a,b,c,d,e,f,j,h,i,j,k)) { // True Code } 
0
source

Compare the same elements in the array:

 same = len(uniq([1,2,3,4])) == 1 
0
source

Source: https://habr.com/ru/post/1342219/


All Articles