I'm not sure if the following statement is possible to write as one line (i.e. Ternary form).
if (A == B)
FunctionA();
else
FunctionB();
Both FunctionA and FunctionB are type void.
See Question&Answers more detail:osI'm not sure if the following statement is possible to write as one line (i.e. Ternary form).
if (A == B)
FunctionA();
else
FunctionB();
Both FunctionA and FunctionB are type void.
See Question&Answers more detail:osNo. The conditional operator is only valid for non-void expressions. The point is to evaluate one of two expressions, and for that to be the result.
Basically: write the if
statement. It's the idiomatic way of executing one action or another.
You could write an extension method like this:
// For demonstration purposes only. Please don't use in real life.
public static void Conditional(this bool result,
Action trueAction,
Action falseAction)
{
Action action = result ? trueAction : falseAction;
action();
}
Then:
(A == B).Conditional(FunctionA, FunctionB);
... but I'd strongly advise you not to.