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
How to use the ?: conditional operator in C#?
The conditional operator (also called the ternary operator) is represented by the symbol ?:. It provides a concise way to write simple if-else statements in a single expression. The operator has right-to-left associativity and evaluates a boolean condition to return one of two possible values.
Syntax
Following is the syntax for the conditional operator −
condition ? value_if_true : value_if_false
Where:
conditionis an expression that evaluates to a boolean valuevalue_if_trueis returned if the condition is truevalue_if_falseis returned if the condition is false
How It Works
The conditional operator works as follows −
The first operand (condition) is implicitly converted to
bool.If the condition evaluates to true, the second operand is evaluated and returned.
If the condition evaluates to false, the third operand is evaluated and returned.
Remember, only one of the last two operands is evaluated in a conditional expression.
Using Conditional Operator for Simple Assignment
Example
using System;
class Program {
static void Main(string[] args) {
int num1 = 100, num2;
num2 = (num1 == 100 ? 200 : 0);
Console.WriteLine("Number One = " + num1);
Console.WriteLine("Number Two = " + num2);
}
}
The output of the above code is −
Number One = 100 Number Two = 200
Using Conditional Operator for String Messages
Example
using System;
class Program {
static void Main(string[] args) {
int score = 85;
string result = (score >= 60 ? "Pass" : "Fail");
Console.WriteLine("Score: " + score);
Console.WriteLine("Result: " + result);
int age = 17;
string eligibility = (age >= 18 ? "Eligible to vote" : "Not eligible to vote");
Console.WriteLine("Age: " + age);
Console.WriteLine(eligibility);
}
}
The output of the above code is −
Score: 85 Result: Pass Age: 17 Not eligible to vote
Nested Conditional Operators
Example
using System;
class Program {
static void Main(string[] args) {
int marks = 92;
string grade = (marks >= 90 ? "A" : marks >= 80 ? "B" : marks >= 70 ? "C" : "D");
Console.WriteLine("Marks: " + marks);
Console.WriteLine("Grade: " + grade);
int temperature = 25;
string weather = (temperature > 30 ? "Hot" : temperature > 20 ? "Warm" : "Cold");
Console.WriteLine("Temperature: " + temperature + "°C");
Console.WriteLine("Weather: " + weather);
}
}
The output of the above code is −
Marks: 92 Grade: A Temperature: 25°C Weather: Warm
Comparison with If-Else Statement
| Conditional Operator | If-Else Statement |
|---|---|
| Single expression, more concise | Multiple statements, more verbose |
| Returns a value directly | Requires explicit assignment/return |
| Best for simple conditions | Better for complex logic |
result = (x > 5 ? "Big" : "Small"); |
if (x > 5) result = "Big"; else result = "Small"; |
Conclusion
The conditional operator ?: in C# provides a compact way to write simple conditional expressions. It evaluates a boolean condition and returns one of two values based on the result. Use it for simple assignments and avoid complex nested conditions for better readability.
