Annalisa Alden
10
Q:

c# ternary operation


// ---------------- Syntax of Ternary Operators ----------------- //

string stateOfMatter;
int temperature = 23;


// ---- For just one condition ---- //
stateOfMatter = temperature < 0 ? "Solid": "Liquid";
  
// If temperature is below zero, then stateOfMatter is solid, otherwise
// it will be liquid
  
  
// ---- For more conditions ---- //
stateOfMatter = temperature < 0 ? "Solid" : (temperature > 100 ? "Gas" : "Liquid");
  
1
is this condition true ? yes : no
4
condition ? consequent : alternative
1
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
1
condition ? consequent : alternative
0

New to Communities?

Join the community