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 −

  • condition is a Boolean expression that evaluates to true or false

  • value_if_true is returned if the condition is true

  • value_if_false is returned if the condition is false

Ternary Operator Flow Condition ? Value if TRUE Value if FALSE : Result TRUE 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.

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements