Ternary Operator (Conditional Operator) รับ 3 Arguments ประเมิน Boolean Expression
Syntax: condition ? value_if_true : value_if_false
ตัวอย่าง:
y = (z == 1) ? 100 : 180;
ถ้า z==1 จริง (True) y = 100 มิฉะนั้น y = 180
ตัวอย่างสมบูรณ์
using System;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
int x, y;
x = 25;
y = (x == 25) ? 20 : 30; // True -> 20
Console.WriteLine("Value of y = {0}", y);
y = (x == 1) ? 50 : 90; // False -> 90
Console.WriteLine("Value of y = {0}", y);
Console.ReadLine();
}
}
}