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>
Franck
source share