C# Ternary Operator


This entry is part 2 of 2 in the series C# Flow Control

The ternary operator is an example of C# flow control. The ternary operator works on three operands. It uses the question mark and the colon. Unary operators that work on one operand, and binary operators that work on two operands. There is only one ternary operator in C#. If you are performing a comparison in C#, the ternary operator is the simplest one to use, in that it requires very little typing and can easily be put on a single line of C# code, making it visually compact.

  • <test> ? <resultIfTrue> : <resultIfFalse>

Here, <test> is evaluated to obtain a Boolean value, and the result of the operator is either <resultIfTrue> or <resultIfFalse> based on this value.

Series Navigation<< C# Flow Control Introduction