If statement - 'or' but NOT 'and'
c#, if-statement
Solution
You could write a helper method. This has the advantage that it short circuits, only evaluating exactly as many as necessary,
public static bool IsExactlyOneTrue(IEnumerable<Func<bool>> conditions) {
bool any = false;
foreach (var condition in conditions) {
bool result = condition();
if (any && result) {
return false;
}
any = any | result;
}
return any;
}
Problem
In C Sharp, how can I set up an if statement that checks if one of several conditions is true? It must be only one of the conditions, if zero or two or more are true the if should be false.