Home > Articles > Programming > C#

  • Print
  • + Share This
From the author of

From the author of

The Ternary Operator

The ternary operator contains three expressions, thus the name "ternary." The first expression must be a Boolean expression. When the first expression evaluates to true, the value of the second expression is returned. When the first expression evaluates to false, the value of the third expression is returned. This is a concise and short method of making a decision and returning a choice based upon the result of the decision. The ternary operator is often called the conditional operator. Here's an example:

long democratVotes  = 1753829380624;
long republicanVotes = 1753829380713;

string headline = democratVotes != republicanVotes ?
         "We Finally Have a Winner!" : recount();
  • + Share This
  • 🔖 Save To Your Account