Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0070 Ternary operator

The ternary operator has the following form: question ? expression1 : expression2 It can be rewritten as: if (question == true){ expression1; }else { expression2; } The following example uses the conditional operator(ternary operator) to get the bigger value: using System; class Program { static void Main(string[] args) { int i = 1; int j = 2; Console.WriteLine(i > j ? i : j); } } The output: 2