Core C#

Conditional Operators

This C# program demonstrates how the conditional operator is used. The conditional evaluates to one of two values, depending on the value of a boolean value.

Program.cs

using System;

namespace XoaX {
    class Program {
        static void Main(string[] args) {
            bool P = true;
            int i = 5;
            int j = 10;
            Console.WriteLine("({0} ? {1} : {2}) = " + (P ? i : j), P, i, j);
            P = false;
            Console.WriteLine("({0} ? {1} : {2}) = " + (P ? i : j), P, i, j);
        }
    }
}
 

Output

(True ? 5 : 10) = 5
(False ? 5 : 10) = 10
Press any key to continue . . .
 
 

© 2007–2024 XoaX.net LLC. All rights reserved.