xxxxxxxxxx
Object obj = null
// is this condition true ? yes : no
var output = (obj == null) ? "Yes" : "No";
// output = "yes"
xxxxxxxxxx
variable = Condition? Expression1 : Expression2
if expression is stated by condition is true = Expression1
if expression is stated by condition is false = Exptession2
public static void Main(string[] args)
{
int number = 11;
String result;
result = (number % 2 == 0) ? "Even number" : "Odd number";
Console.WriteLine(result);
}
xxxxxxxxxx
conditional-expression:
conditional-or-expression
conditional-or-expression ? expression : expression
xxxxxxxxxx
using System;
namespace Operator
{
class TernaryOperator
{
public static void Main(string[] args)
{
int number = 10;
string result;
result = (number % 2 == 0)? "Even Number" : "Odd Number";
Console.WriteLine("{0} is {1}", number, result);
}
}
}
xxxxxxxxxx
double sinc(double x) => x != 0.0 ? Math.Sin(x) / x : 1;
Console.WriteLine(sinc(0.1));
Console.WriteLine(sinc(0.0));
// Output:
// 0.998334166468282
// 1