How to determine if three ints are equal

Hi, I have three ints: value1, value2 and value3.

What is the best way to determine if they are all the same?

I tried:

return value1 == value2 == value3 

But that said:

The operator '==' cannot be applied to operands of type 'bool' and 'int'.

So, I think he compares the first two, which return a boolean that he is trying to compare with the third.

I could go:

 return value1 == value2 && value2 == value3; 

But it seems to be becoming untidy.

Anyone have a good suggestion?

+8
c #
source share
6 answers

The second seems very good to me.

As the list gets longer, it can become cumbersome. In this case, I would write an AllSame line extension method.

 bool AllSame(this IEnumerable<int> list) { bool first = true; int comparand = 0; foreach (int i in list) { if (first) comparand = i; else if (i != comparand) return false; first = false; } return true; } 

or use the params :

 bool AllSame(params int[] list) { return (list as IEnumerable<int>).AllSame(); } 

Then you can simply write:

 if (AllSame(value1, value2, value3, value4, value5)) ... 
+5
source share

I changed my original answer to include a method that is more general and that does not depend on LINQ or extension methods. I consider it safe to assume that this method will be more effective, based on the fact that it does not need to list the entire list in order to determine uniqueness when the list contains values ​​that differ at an early stage.

 class Program { static void Main(string[] args) { int value1 = 1, value2 = 2, value3 = 1; Console.WriteLine(AllAreEqual<int>(value1, value2, value3)); Console.Write("V2: 1 value "); Console.WriteLine(AllAreEqual_V2<int>(1)); Console.Write("V2: no value "); Console.WriteLine(AllAreEqual_V2<int>()); Console.Write("V2: 3 values, same "); Console.WriteLine(AllAreEqual_V2<int>(1, 1, 1)); Console.Write("V2: 3 values, different "); Console.WriteLine(AllAreEqual_V2<int>(1, 1, 2)); Console.Write("V2: 2 values, same "); Console.WriteLine(AllAreEqual_V2<int>(1, 1)); Console.Write("V2: 2 values, different "); Console.WriteLine(AllAreEqual_V2<int>(1, 2)); Console.ReadKey(); } static bool AllAreEqual<T>(params T[] args) { return args.Distinct().ToArray().Length == 1; } static bool AllAreEqual_V2<T>(params T[] args) { if (args.Length == 0 || args.Length == 1) { return true; } if (args.Length == 2) { return args[0].Equals(args[1]); } T first = args[0]; for (int index = 1; index < args.Length; index++) { if (!first.Equals(args[index])) { return false; } } return true; } } 
+4
source share

That seems good to me. The only comment I have is that you must enter an “explanatory variable” for the equation. In addition to explaining the calculation, return now provides a good place for a breakpoint or breakpoint when checking the result.

 bool allThreeAreEqual = value1 == value2 && value2 == value3; return allThreeAreEqual; 
+3
source share

If you are just looking for elegance (given that you already have a solution in which there is nothing wrong), you can go with good'ol LINQ. It can handle three or more.

 class Program { static void Main(string[] args) { List<int> mylist = new List<int>(); mylist.Add(1); mylist.Add(1); mylist.Add(1); mylist.Add(1); bool allElementsAreEqual = mylist.All( x => ( x == mylist.First() )); } } 
0
source share
 int nOneInput = 5; int nTwoInput = 5; int nThreeInput = 5; if ((nOneInput + nTwoInput + nThreeInput ) / 3 == nOneInput ) { // all 3 sides are equal when... // the sum of all 3 divided by 3 equals one of the values } 
0
source share

Using LINQ, we would be better off Any() . Why Any() instead of All() is that All() will check the predicate on all elements of the collection, and Any() will exit as soon as the element matches the predicate.

Here I use the reverse check. I am looking for any item that will be different from the "a" element. Therefore, as soon as he finds one another, we know that they are not all equal, so he goes out and returns the truth. Thus, he will only check items "b", "c" and "d".

 // all values to compare var a = 4; var b = 4; var c = 4; var d = 8; var e = 6; var f = 4; // return if any of the following is different and negate to get a true var areSame = (!new[] { b, c, d, e, f}.Any(i => i != a)); 

If you override equals, you can create generic extensions from this that are reusable and can work for several types if they implement IEqualityComparer<T>

0
source share

All Articles