Mega Code Archive

 
Categories / C# Tutorial / Operator
 

The Operator

The ? operator is often used to replace certain types of if-then-else constructions. The ? is called a ternary operator because it requires three operands. It takes the general form Exp1 ? Exp2 : Exp3; where Exp1 is a bool expression, and Exp2 and Exp3 are expressions. The type of Exp2 and Exp3 must be the same. The value of a ? expression is determined like this: Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire ? expression. If Exp1 is false, then Exp3 is evaluated, and its value becomes the value of the expression.