Conditional Operator in C#
Notes:
Conditional Operator in C# Programming Language:
Conditional Operator ( ?: ): is the ternary operator in C#. It accepts 3 operands.
(operand1) ? operand2 : operand3;
Where:
operand1: must be a condition; which must be evaluated to true or false
operand2: value // true part
operand3: value // false part
Ex: Program to find the given number is even or odd
using System;
namespace ConditionalOperatorDemo
{
class Program
{
static void Main(string[] args)
{
int num = 10;
Console.WriteLine(num + " is " + ((num%2==0) ? "even" : "odd"));
Console.ReadKey();
}
}
}