Method Call using Ternary Operator

The ternary operator is used to return values and those values must be assigned. Assuming that the methods doThis() and doThat() return values, a simple assignment will fix your problem.

If you want to do what you are trying, it is possible, but the solution isn't pretty.

int a = 5;
int b = 10;
(a == b ? (Action)doThis : doThat)();

This returns an Action delegate which is then invoked by the parenthesis. This is not a typical way to achieve this.


Ternary operator must return something. A typical usage is like this:

int x = (a > b) ? a : b;

If you try something like

a + b;

The compiler will complain.

(a > b) ? a - b : b - a;

is basically a shortcut for either "a - b" or "b - a", which are not legitimate statements on their own.