Ternary Operator in C#
C# decision-making operator includes also ternary operator?:
. It is the short form of the if else conditions.Syntax:
condition ? statement 1 : statement 2
Example
using System;
public class Program
{
public static void Main()
{
int x = 10;
var resultFirstExample = x % 2 == 0 ? "Number is even" : "Number is odd";
Console.WriteLine("{0} {1}", x, resultFirstExample);
int y = 11;
var resultSecondExample = y % 2 == 0 ? "Number is even" : "Number is odd";
Console.WriteLine("{0} {1}", y, resultSecondExample);
}
}
Output
10 Number is even
11 Number is odd
11 Number is odd