You want to compare two values and produce a third value that depends on the result of the comparison. Although this can be accomplished with if-statements or other constructs, the ternary operator in the C# programming language provides an elegant and equivalent solution to this requirement. Here we look at examples of the ternary operator and ternary expressions in action, using the C# programming language targeting the .NET Framework
Ternary expression initialization:
First, one common use of the ternary operator in the C# language and other C-like languages is to initialize a variable with the result of the expression. However, the high-level ternary statement is useful because it allows you to condense multiple if-statements and reduce nesting. It does not actually eliminate branching but simplifies the high-level representation of the branching at the level of the source code.
Example: #1
using System;
class Program
{
static void Main()
{
//
// If the expression is true, set value to 1.
// Otherwise, set value to -1.
//
int value = 100.ToString() == "100" ?
1 :
-1;
Console.WriteLine(value);
}
}
Question mark and colon characters.
The program text above shows how the ternary expression can be used when initializing a variable. If the subexpression (100.ToString() == "100") evaluates to true, the integer variable with the identifier 'value' has its location on the stack copied to the bit values of the integer 1. Otherwise, it has its location on the stack copied to the binary representation of -1. Internally, this code uses branch statements that are defined in the intermediate language.
Multiple Ternary Operator
Here we look at how you can check multiple condition and return multiple values respectively. following example shows you that it is possible in c# programming.
Example: #2
using System;
class Program
{
static void Main()
{
int nStatus = MethodCall(text);
int nUserStatus = (nStatus == 0 && !checkStatus.Checked) ? nStatus = 1
: (nStatus != 0 && checkStatus.Checked) ? nStatus = 0
: (nStatus == 0 && checkStatus.Checked) ? nStatus = -1
: (nStatus != 0 && !checkStatus.Checked) ? nStatus = -99
: nStatus;
}
}
The above program gets the value from a method call and sets different value based on the multiple condtions.
Ternary min and max
Here we note that one popular use of the ternary operator in C-like languages is to get the minimum or maximum of two numbers or one variable and an integer constant. This approach is still useful in the C# language, but the Math class in the .NET Framework provides the Math.Min and Math.Max methods, which handle certain cases more effectively and may have clearer calling syntax. The decimal type in the .NET Framework is handled separately by the Math.Max and Math.Min methods, and for this type the ternary expression may not be equivalent.
Implementation
Here we mention some details of how the ternary operator in the C# language can be translated by the C# compiler into the intermediate language. The author disassembled several versions of ternary expressions and found that they are identical to if-statements, with one small difference. The ternary statement sometimes produces code that tests the opposite condition that you would expect, as in it tests that the subexpression is false instead of testing if it is true. This reorders some of the instructions and can occasionally boost performance.
Summary
Here we looked at the ternary operator in the C# programming language, using it to initialize an integer variable and then using it to return a value. We noted that the ternary statement can receive an expression that it first evaluates before proceeding to part after the question mark. We reviewed some aspects of the intermediate language and some additional uses of the ternary statement and how it can produce branches in your control flow.
Rajan Chellappan