Monday, 16 December 2019

C# - ?: operator Or Ternary Conditional Operator


(MSDN)
The conditional operator ?:, also known as the ternary conditional operator, evaluates a Boolean expression and returns the result of one of the two expressions, depending on whether the Boolean expression evaluates to true or false. Beginning with C# 7.2, the conditional ref expression returns the reference to the result of one of the two expressions.

condition ? consequent : alternative

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

Example
C#
int value = 15;
value = value % 26 == 0 ? 1 : value;

Example
VB
Dim value As Integer = 15
value = If((value Mod 26) = 0, 1, value)


No comments:

Post a Comment