The Conditional Operator

As shown in Syntax Box 8.12 the conditional operator combines three expressions with the two symbols—question mark ? and colon :. It will return either the value of ExpressionA or the value of ExpressionB, depending on whether the Boolean expression to the left of the question mark is true or false. If true, the value of ExpressionA is returned; otherwise, the value of ExpressionB is returned.

Syntax Box 8.12 The Conditional Operator

						<Boolean_expression> ? <ExpressionA> : <ExpressionB>
					

Note

The conditional operator is the only operator in C# to combine three expressions, so it is also referred to as the ternary operator. ...

Get C# Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.