Ternary operator in C#

Posted by Techie Cocktail | 12:48 AM | | 0 comments »

Ternary operator is often used to replace the if-then-else statements.

syntax
Expression1 ? Expression2 : Expression3

where,
Expression1 is like 'if condition'
Expression2 takes place of 'then'
Expression3 takes place of 'else'

See below example for clear understanding.


class operatorClass
{
public static void Main()
{
string output;

output = 20 > 10 ? "20 greater than 10" : "10 greater than 20";
Console.WriteLine("result = " + output);//(result = "20 greater than 10")
}
}

0 comments