C# Ternary Operator

C# has an unusual operator that is called Ternary Operator. It is a combination of? and: and takes three operands. This operator is also known as a conditional operator. It has the following syntax:

conditional expression? expression1:expression2

The operator ?: works as follows: exp1 is evaluated first. If it is true, then the expression exp2 is evaluated and becomes the value of the conditional expression. If exp1 is false, exp3 is evaluated and its value becomes the value of the conditional expression.

Example:

a=25;
b= 30;
x=(a>b)?a:b;

In this example, x will be assigned the value of b. It can be achieved using the if.. else statement as follows:

if(a>b)
x=a;
else
x=b;