Alex Rivera | Logout

How can I test that multiple booleans have identical values in c#?

Asked 2010-11-10T07:07:50.443
9

Is there a shorter way of testing the following:

if (!((a == b) && (a == c) && (a == d) && (a == e) && (a == f)))

I ended up writing a method that lets me do this

if (!AllEqual(new[] { a, b, c, d, e, f }))

That feels nicer and is a lot more readable, but I'm wondering if there is something in the framework that does this already?

Edit
Report

1 Answer

1

You could distribute the ! into the expression:

if ((a != b) || (a != c) || (a != d) || (a != e) || (a != f)))

You could also use Any or ! All rather than adding a new method.

if (new[]{b, c, d, e, f}.Any(x => x != a)) ...
answered 2010-11-10T07:13:19.320

Your Answer