5

I want to check whether these variables have 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 all are same?
LIKE in below example

if(a==b==c==d==e.....j==k)
{
    //Understandable but not work 
}
Javed Akram
  • 14,220
  • 23
  • 77
  • 113

12 Answers12

10

how about something like this:

if (Array.TrueForAll<int>(new int[] {a, b, c, d, e, f, g, h, i, j, k },
        val => (a == val))) {
    // do something
}
mellamokb
  • 53,762
  • 11
  • 101
  • 131
4

You could create a var args method to do that:

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 ints as parameters:

if (intsEqual(a, b, c, d, e, f, g, h, i, j, k)) {
    doSomeStuff();
}
Kaleb Brasee
  • 48,461
  • 8
  • 103
  • 110
  • Isn't that supposed to be ints.Length and i < ints.Length - 1? – leiz Mar 04 '11 at 02:05
  • 1
    Yeah probably, I almost never use C#. – Kaleb Brasee Mar 04 '11 at 02:06
  • The second one is off-by-1 error and it is nothing to do with c# :) – leiz Mar 04 '11 at 02:08
  • LOL, this is true. I actually create methods like this quite a bit, mostly for convenience methods in test cases. But it's always in Java or Groovy, so I'm not used to this crazy .NET syntax. – Kaleb Brasee Mar 04 '11 at 02:09
  • @Kaleb: crazy .NET syntax?!? Wanna fight about it? :) – mellamokb Mar 04 '11 at 02:15
  • You bet! I'm always willing to defend the opening braces on same line (and Steve Mcconnell will back me up)!! LOL. At least we don't have an end statement like Ruby. – Kaleb Brasee Mar 04 '11 at 02:25
  • @Kaleb: For the record, VS supports opening braces on the same line as well, you just have to modify the setting: http://stackoverflow.com/questions/4020070/can-i-make-visual-studio-place-curly-braces-on-the-same-line-as-an-if-statement. – mellamokb Mar 04 '11 at 13:29
4

With this many variables, would it make sense to move them into an array?

You could then test to see if they are all equal using Linq expressions like myarray.Distinct().Count() == 1; or perhaps myarray.All(r => r == 5);

dthorpe
  • 33,791
  • 3
  • 72
  • 118
2

I agree that the easiest way is to place them all into a list and then use the following to compare. This is in essence looping through and comparing to the first value, but this is a little cleaner.

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

Just a thought, but if you can calculate the standard deviation of the entire list, and it is equal to zero, you would have your answer.

Here's an answer on the site that addresses this that may help with that: Standard deviation of generic list?

Interesting problem. Good luck with it.

Community
  • 1
  • 1
nycdan
  • 2,681
  • 2
  • 19
  • 32
1

How about

int common = a;
if (a==common && b==common  && c==common  && d==common  && .... && k==common)
user534498
  • 3,596
  • 2
  • 22
  • 45
1

You could write a helper method like this:

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;    
}

This will be subtly different to the == operator in special cases, though:

AllEqual (double.NaN, double.NaN).Dump();    // True
(double.NaN == double.NaN).Dump();           // False
Joe Albahari
  • 28,102
  • 6
  • 74
  • 88
0

I know it's an old question I came across but I was wondering what's wrong with:

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

}
Shakti Prakash Singh
  • 2,306
  • 4
  • 31
  • 58
0

It doesn't work because a==b evaluates to a boolean which can't be compared to an integer, c. What you have seems to be the best way.

Brandon Frohbieter
  • 15,944
  • 3
  • 33
  • 60
0

You might consider putting the values in an array and using a for() loop. It isn't really any simpler, but it might help if the number of values changed.

David Winant
  • 812
  • 4
  • 8
0

You could use a variable argument helper function to perform the comparison pretty easily.

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 would just use the function as follows

if(CompareLongList(a,b,c,d,e,f,j,h,i,j,k))
{
   // True Code
}
iwalkbarefoot
  • 906
  • 7
  • 14
0

Compare the same elements in array:

same = len(uniq([1,2,3,4])) == 1
iEfimoff
  • 147
  • 1
  • 6