Is a ternary expression possible for this construct?

No. 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.

Leave a Comment