Home  >  Article  >  Backend Development  >  C# Ternary Operators

C# Ternary Operators

PHPz
PHPzOriginal
2024-09-03 15:09:04697browse

The operators used for decision making which replaces the conditional statements if and else are called a ternary operator in c#, which consists of three arguments among which the first argument is used for comparison and if the result of this comparison is true, the second argument is returned, it the result of the comparison is false, the third argument is returned, and this operator can be thought of as an optimized way of using if-else statement.

Syntax:

Boolean Expression ? First statement : Second statement

The above syntax represents the ternary operator. It consists of three parts. The first part of the syntax is before ?. It returns a Boolean value true or false because it is a conditional expression. The second part of the syntax is before ‘?’ and after ‘:’, which is executed if the result of the first part’s conditional expression is true. The third part of the syntax after the ‘:’ statement is executed if the result of the conditional expression that is the first part is false.

Working of C# Ternary Operators

Following are the different examples of ternary operators.

1. Comparison of Two Values Using If Else

Consider the below C# program for comparison of two values using the if-else statement.

Code:

using System;
namespace Project
{
class MainClass
{
public static void Main(string[] args)
{
int a = 30;
int b = 40;
if (a < b)
{
Console.WriteLine("a's value is less than b");
}
else
{
Console.WriteLine("b's value is less than a");
}
}
}
}

Output:

C# Ternary Operators

In the above program, two variables, a and b, are defined and assigned some values. Their values are compared against each other to find out which is greater using if-else conditional statements. The above program consists of twenty lines of code to compare two values against each other. The same code can be optimized to fourteen lines by using the ternary operator, which is used in the code below:

2. Comparison of Two Values Using Ternary Operators

Consider the below C# program for comparison of two values using ternary operators.

Code:

using System;
namespace Project
{
class MainClass
{
public static void Main(string[] args)
{
int a = 40;
int b = 30;
Console.WriteLine((a < b) ? "a's value is more than b" : "b's value is less than a");
}
}
}

Output:

C# Ternary Operators

In the above program, two variables, a and b, are defined and assigned some values. Their values are compared against each other to find out which is greater using ternary operators. The conditional statement is executed, and the result of the statement is assigned to a variable res. If the conditional statement’s result is true, the second statement after the ‘?’, which is a’s value is more than b, is printed; otherwise, the third statement after the ‘:’  b’s value is less than a is printed.

3. Nested Ternary Operator

When the second argument or the third argument after ‘?’ or after ‘:’ is a conditional statement again, then the operator is called the nested ternary operator. Consider the below program, for example:

Code:

using System;
namespace Project
{
public class MainClass
{
public static void Main(string[] args)
{
int a = 10;
int b = 8;
Console.WriteLine(a> b ? "a's value is more than b" : a < b ? "a's value is less than b" : a == b ? "C" : "No result");
}
}
}

Output:

C# Ternary Operators

In the above program, two variables, a and b, are defined and assigned some values. Their values are compared against each other to find out which is greater or if they are equal using ternary operators in C#. The conditional statement is executed, and the result of the statement is assigned to a variable res. If the result of the conditional statement is true, the second statement after the ‘?’ which is again a conditional statement a Note: Ternary operators cannot execute statements. It only returns an expression or value present in the second part or third part depending on the conditional statement’s result in the first part.

Examples of C# Ternary Operators

Following are the different examples of ternary operators in C#.

Example #1

C# program using nested ternary operator to find out the largest of the given numbers.

Code:

using System;
using System.IO;
using System.Text;
//Define Namespace
namespace program
{
//Define class
public class large
{
//Define main method
public static void Main(string[] args)
{
//Define three variables to take the input
int x;
int y;
int z;
//Get the input from the users
Console.Write("First number must be entered : ");
x = Convert.ToInt32(Console.ReadLine());
Console.Write("Second number must be entered: ");
y = Convert.ToInt32(Console.ReadLine());
Console.Write("Third number must be entered : ");
z = Convert.ToInt32(Console.ReadLine());
//largest number is found out by using nested ternary operator
int large = (x>y)?((x>z)?x:z):(y>z?y:z);
//display the largest number
Console.WriteLine("{0} is the largest number", large);
Console.ReadLine();
}
}
}

Output:

C# Ternary Operators

Example #2

C# program using the ternary operator to find out if a given number is even or not.

Code:

using System;
// Define Namespace
namespace program
{
// Define class
public class check
{
// Define main method
public static void Main(string[] args)
{
//Assign the number which need to be checked if it is even or not
int number = 8;
bool ifitisEven;
//Logic to check if ithe given number is even or not
ifitisEven = (number % 2 == 0) ? true : false ;
Console.WriteLine(ifitisEven);
}
}
}

Output:

C# Ternary Operators

Conclusion

In this tutorial, we understand the ternary operators’ concept in C# through definition and then understand the working of ternary operators in C#. Then we understand different C# programs using the nested ternary operator and simple ternary operator and their working using programs with their output snapshots included with the results of the programs in it.

The above is the detailed content of C# Ternary Operators. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# OR OperatorNext article:C# OR Operator