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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.