I would like to compare 2 equations
, lets say 2*2 and 2+2
to a single answer, 4
, using Visual Studio 2015, e.g.
if (4 == (2*2 && 2+2)) {...}
but it returns an error, "Operator '&&' cannot be applied to operands of type 'int' and 'int'". The only other way I could think of writing the code is:
if (4 == 2*2 && 4 == 2+2) {...}
which would work, but gets very repetitive when a lot of values are to be compared. Is there a easier way to achieve this?
var results = new[]{ 2 + 2, 2 * 2, ... };
if (results.All(r => r == 4)) {
...
}
This gathers the results of all operations in the collection results
, and uses the extension method All
to verify that the specified predicate holds for all values; allowing to write the predicate only once.