Ternary Operator

noun

say it: “TER-nuh-ree OP-er-ay-ter” /ˈtɜːrnəri ˈɒpəreɪtər/

The ?: operator, which picks one of two values depending on a condition, in a single expression.

What is the ternary operator?

A single line if/else statement.

double chocolate = 2.50;
double donuts = 3.99;

double maxPrice = (chocolate > donuts) ? chocolate : donuts;

In a sentence

  • “I used the ternary operator: string label = wins > losses ? "Winning" : "Losing";”

Level: Apprentice · More C# terms at this level

Updated

In other subjects