c sharp

C# Ternary

As we know, there are various conditional statements in C#. The ternary operator is also a kind of conditional statement. In “if-else”, if the condition is true, then the statement is written in curly braces below “if” will execute; otherwise, the else part will be rendered. In the ternary operator, we have a condition and two statements to be evaluated. If the condition is true, then the statement on the left side of the colon “:” is evaluated, and the condition is not satisfied, then the statement on the right side of the colon “:” is evaluated, the same as we have in the “if-else” condition. The ternary operator shrinks the size of the “if-else” statement. It helps to decrease the size of the program. In this article, we will check and learn how the ternary operator is used in C# programming in Ubuntu 20.04.

Syntax of “Ternary Operator” in C#

The syntax of the ternary operator is below here:

# (Condition)? Statement1: statement2

In the ternary operator, we see that we have a condition and two statements. The condition is a Boolean condition. After this condition, we have two statements separated by a colon “:” when the given condition is checked, and it is true, then the statement ahead will be executed, and if the condition is not true, then the statement on the right side of the colon “:” will be executed.

Now, we have different examples here in this article to understand the working of this ternary operator. Let’s have a look at these examples, how it will check the condition and will display the statement for true or false results of the condition.

Example 1

In this given example, we have a simple code to understand the working of the ternary operator in C# programming in Ubuntu 20.04.

In this code, first, we have “using System”. It’s a mandatory part of the C# program. We import this library to get access to the functions and methods we use in this C# program. Also, we have a namespace “Program”. We created a class named “Ternary”. Inside this class, we invoked a function which is the “main” function of the C# code “static void Main(string[] args)”. For representing the arguments of this function, (string[] args) is used. In this main function, we declare and initialize two integers, “int a” and “int b”, and assign values “17 and 25”, respectively. After this, we have declared a variable named “comparison” with the “string” data type.

We assign the ternary operator to this string data type variable “comparison”. And the result of this conditional statement is stored in this “comparison”. In a conditional statement, we have a condition that a is greater than b (a>b). if this condition is true, then the statement ahead on the left side of the colon “:” will be stored in the “comparison”. Otherwise, in case of a false condition, the statement on the right side of the colon will be stored in the “comparison”. We use the “ConsoleWriteLine(comparison)” statement to display this result on the screen.

Output
For displaying the output of this C# code, we use “mcs” and “mono” keywords with the filename and the extension of “.cs” and “.exe” respectively in Ubuntu 20.04. The image is here for you to understand how to get the output of the C# program in Ubuntu 20.04.

In this output, we see that it prints “b is greater than a” as we assign “17” to “a” and “25” to b. The ternary operator checks both values and displays the result on the screen.

Example 2: By Getting Input from User in C# Program in Ubuntu 20.04.

Here, we have a code in which we get the input data from the user and then use these users’ input in a ternary operator and display the true result.

We have “using System” in the above code, which we discussed in our previous code. Now, we are using the namespace as “Numbers”. After this, we created a class with the name “User”, and inside the class, we have the “main” function. We have discussed all these in the above example. Now, we move to the next line of code in which we declare four variables with the integer data type. We get all these integer values to form the user and get input from the user.

Before the user input, we print a line on the screen “Enter Four Numbers of your choice:” with the help of “Console.WriteLine()”. When this line renders on the screen, the user will know that he/she has to input four numbers. We assign the first user input to “m” by using “int. Parse(Console.ReadLine())”, same as we assign the second user input to “n”, third and fourth input to “o” and “p” respectively. After this, we have declared two new integers named “a1” and “a2” and have two conditional statements. We assign the conditional statement “(m>n? m:n)” to “a1” and “(o>p? o”p) to “a2”.

The result of the first conditional statement “(m>n? m:n)” is stored in “a1”, and the result of the second conditional statement “(o>p? o”p) is stored in “a2”. Now, after this, we have another variable named “greater” with an integer data type, and we assign the third conditional statement to this “greater” variable. Same as above, the ternary operator “(a1>a2? a1:a2)” is executed in this “a1” value is the result of the first ternary operator, and the “a2” value is the result of the second ternary operator, which is checked in this ternary operator. The result will be stored in this “greater” variable. For displaying the result of this “greater” variable, we have the “Console.WriteLine()” function.

We show the output of this code in Ubuntu 20.04 terminal in this given image.

This output shows that it prints the line first so that the user enters four numbers. After this, when the user enters the numbers, the program displays the result on the screen. As we see, in this user input, the greatest value is “99”.

Example 3: By Nested Ternary Operator in C# in Ubuntu 20.04

Now, in this example, we learn how to use the nested ternary operator in C# programming in Ubuntu 20.04. We have another ternary operator inside the ternary operator in the nested ternary operator. We will check how this nested ternary operator works in the given example.

In the first line, we have “using System”. After this, we use the namespace as “Ternary” and a class named “Nested”. Also, it invoked a “main” function in this class. Inside this “main” function, we have declared and initialized two integers named “x” and “y” and assigned “55” to both of them. Then, we declare “result” as a string data type. The variable “result” contains the nested ternary operator in which the first condition is checked, which is “(x>y)” if the condition is true, then the statement on the left side will be executed; otherwise, it moves to the right side of “:”, where we have another ternary operator.

The condition of this nested ternary operator is “(x<y)”, which is checked and then executed accordingly. The true result is stored in “result”. The “Console.WriteLine()” is used to display the data stored in “result”. The “ConsoleReadLine()” is here to read the string.

The output of this above code is here in this image. We get the output using the same commands as in the above examples; just the filename is different.

Conclusion

In this guide, we have learned how to implement the ternary and nested ternary operators in the C# programming language. Here we studied in detail the ternary operator in C#, so by using this method, we can write complex programs as well. We see it’s a conditional operator containing a Boolean condition. It evaluates true and false results the same as the if-else statement. These examples help you understand the concept of the ternary operator and how it works. These are some basic examples. You can use these examples to implement a complex C# program.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.