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.

Leave a Comment