The ? Operator : Ternary Operator : Operator C# Examples


C# Examples » Operator » Ternary 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.




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Operator
» Ternary Operator