To evaluate situations and determine the flow of a program, the If-else conditions and Switch cases are utilized. The ternary operator is a conditional statement shorthand operator that can be used to reduce the length of conditional statements. The ternary operator, when used correctly, can minimize code size and improve readability. The ternary operator has no particular keyword; it’s the technique of an if-else statement is written that forms a ternary expression or conditional statement. In the article, we are going to use the ternary operator in C# programming and know the benefit of using the ternary operator in C# programming. In C#, the ternary operator refers to the decision-making operators that replace the conditional expressions if and else.
What is Ternary Operator ( ? : ) in C# in Ubuntu 20.04?
The ternary operator ( ? : ) is a conditional operator that is used to make a simple comparison or check on a set of simple expressions? It shortens the code used to conduct conditional operations. The left to right is followed by the ternary operator. This operator has three arguments: the first is used for comparison, the second is returned if the true Boolean value is the comparison result, and the third parameter is returned if the comparison result is false, and it can be considered an optimal way of using the if-else statement.
Syntax of the Ternary Operator ( ? : ) in C# in Ubuntu 20.04
The following is the syntax for implementing a Ternary Operator in the C#:
Condition? statement1: statement2The condition here is evaluated and returns the Boolean value. Depending on the value of a condition, the ternary operator ( ? : ) should return only one value from the specified statements, either the first statement or the second statement. Then, after the “?” comes the statement1, which is executed if the result is true. The statement2 is placed after the “:” and executed when the result is false.
To understand the basics of using the ternary operator ( ? : ) in C#, we have demonstrated some examples of code implementation, and the code is compiled using a mono compiler in Ubuntu 20.04:
Example # 1: Program of Using the Ternary Operator ( ? : ) in C# in Ubuntu 20.04
The ternary operator is demonstrated in the following example in the C# programming language.
We have to import the “using system” C# system library so that we can access the C# functions and methods required. The class is created as “MyProgram” and kept public. In the class “MyProgram” block, we have invoked a void main function. The void main function has a variable “a” and “b” is declared as an integer data type. These variables are initialized with some numeric values. The other variable is defined as the keyword “var” and declared as “Result”.
The ternary operator is applied to the variables “a” and “b” in the variable “Result.” We have a condition first that “a” should be less than “b”. Then, if the result is true according to the given condition the first expression is returned which is placed before the “:”, otherwise the second expression after “:” is returned. The WriteLine method is invoked for printing the values given to the variables “a” and “b”. Also, the Result value is printed through the C# writeLine method.
The first expression is executed in the output as you can see that the value assigned to “a” is less than the value assigned to a variable “b”.
Example # 2: Program of Using the Nested Ternary Operator ( ? : ) in C# in Ubuntu 20.04
We can make a Nested Ternary Operator in C# by using numerous conditional expressions as the second or third part of the ternary operator’s expressions. In the C# programming language, we can use nested ternary operators to replace if…else if expressions.
Initially, we have to import the C# system library for implementing the C# methods and functions which we have imported. The class is defined as “MyProgram” and in the class, we have called the void main function for code implementation. The integer data type is used for the variables defined as “i” and “j”. These variables have assigned the values respectively. Then, we have another variable declaration which is of String data type. The variable is named “Result” and we have used a nested ternary operator in this variable.
Firstly, we have a condition that “i” should be greater than “j” before the “?” operator. After that, we have our first statement to be executed when the condition is true. Then, we have another condition that “i” should be less than “j” and the second statement is executed if this condition is true. At last, we have the condition when we have “i” equals “j”. For this, the third statement is executed otherwise the last statement is executed when these conditions specified are not true. The writeLine method is invoked for printing first the values of the variables “i” and “j” and then printing the executed statement from these particular conditions by the “Result” variable.
As you can see in the output, the values assigned to both the variables “i” and “j” are the same so the third statement is true and displays the output message in the image below.
Example # 3: Program of Using the Ternary Operator ( ? : ) to Replace the If-Else Block in C# in Ubuntu 20.04
Instead of utilizing the if-else block in the C# code implementation, we utilized the ternary operator to check whether the integer is even or odd.
In C# programming, the system library is included in the code first. The namespace as “TernaryOperatorProgram” is used here through which code is identified and encapsulated. Then, we have created a class “TernaryOperator” and in the class, we have called the static void main function. In the static void main function, we have a variable declaration as “num” and set the data type integer for this variable. A value of “5” is assigned to the variable “num.” The bool data type is also defined for the variable “IsEven”. The “IsEven” variable is using the ternary operator which checks whether the value of “num” is even or not.
The conditional expression for checking the even value is placed before the ternary operator. The only one expression whether true or false is returned according to the conditional statement. The writeLine method will print the resultant value as we have passed “IsEven” to it.
The output from the above ternary operator conditional statement is shown on the terminal screen.
Example # 4: Program of Using Ternary Operator ( ? : ) to Prevent Division by Zero in C# in Ubuntu 20.04
The following program divides the number but prevents the division by zero. We have used a ternary operator which does not allow the division by zero.
The class “MyProgram” is defined here and in the class, we have invoked the void main function. The variable is defined as the “Result” of the data type integer. Then, we have a for loop method in which we have assigned a value to a variable “num”. The “Result” returned the output of the division operation. The condition is specified here that “num” is only divided by 100 if it is not zero. The writeLine method returns the outcome of “Result”.
The result from the above program is seen in the image below.
The purpose of this article is to show how to utilize the ternary operator in C# instead of the if-else expression. The ternary operator is not time-consuming and concise in the code implementation. It’s all about the conditional expressions that will be run if the condition is true or false. We have implemented a few examples of the ternary operator in C# programming. The article will give you a better understanding and clear concept regarding the term “ternary operator” in C#.