Object obj = null
// is this condition true ? yes : no
var output = (obj == null) ? "Yes" : "No";
// output = "yes"
is this condition true ? yes : no
conditional-expression:
conditional-or-expression
conditional-or-expression ? expression : expression
static void Sample(string input)
{
string result = input == null ? "default" : input;
Console.WriteLine($"Result: {result}");
}
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);
}
}
}
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