Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Ternary Operator in C#
The ternary operator in C# is a conditional operator that provides a concise way to evaluate expressions based on a Boolean condition. It takes three operands and returns one of two values depending on whether the condition is true or false.
The ternary operator is also known as the conditional operator and uses the syntax condition ? value_if_true : value_if_false. It's particularly useful for simple conditional assignments and can make code more readable when used appropriately.
Syntax
Following is the syntax for the ternary operator −
result = condition ? value_if_true : value_if_false;
Where −
-
conditionis a Boolean expression that evaluates to true or false -
value_if_trueis returned if the condition is true -
value_if_falseis returned if the condition is false
Basic Example
using System;
class Program {
static void Main(string[] args) {
int a, b;
a = 10;
b = (a == 1) ? 20 : 30;
Console.WriteLine("Value of b is {0}", b);
b = (a == 10) ? 20 : 30;
Console.WriteLine("Value of b is {0}", b);
}
}
The output of the above code is −
Value of b is 30 Value of b is 20
Using Ternary Operator with Different Data Types
Example
using System;
class Program {
static void Main(string[] args) {
int age = 18;
string status = (age >= 18) ? "Adult" : "Minor";
Console.WriteLine("Status: " + status);
double score = 85.5;
string grade = (score >= 90) ? "A" : (score >= 80) ? "B" : "C";
Console.WriteLine("Grade: " + grade);
bool isEven = (10 % 2 == 0) ? true : false;
Console.WriteLine("Is 10 even? " + isEven);
}
}
The output of the above code is −
Status: Adult Grade: B Is 10 even? True
Nested Ternary Operators
Ternary operators can be nested to handle multiple conditions, though this should be used sparingly for code readability −
Example
using System;
class Program {
static void Main(string[] args) {
int temperature = 25;
string weather = (temperature > 30) ? "Hot" :
(temperature > 20) ? "Warm" :
(temperature > 10) ? "Cool" : "Cold";
Console.WriteLine("Temperature: {0}°C - Weather: {1}", temperature, weather);
temperature = 35;
weather = (temperature > 30) ? "Hot" :
(temperature > 20) ? "Warm" :
(temperature > 10) ? "Cool" : "Cold";
Console.WriteLine("Temperature: {0}°C - Weather: {1}", temperature, weather);
}
}
The output of the above code is −
Temperature: 25°C - Weather: Warm Temperature: 35°C - Weather: Hot
Ternary Operator vs if-else Statement
| Ternary Operator | if-else Statement |
|---|---|
| Single line expression | Multi-line block statement |
| Returns a value directly | Executes statements |
| Best for simple conditions | Better for complex logic |
| More concise for assignments | More readable for beginners |
Conclusion
The ternary operator in C# provides a concise way to perform conditional assignments and evaluations. It's most effective for simple conditions and can make code more readable when used appropriately, though complex nested ternary operations should be avoided in favor of traditional if-else statements.
