What is the Ternary Operator in C#?

💡 Concept: Ternary Operator (?:)

The ternary operator in C# is a shorthand for an if-else condition. It evaluates a condition and returns one of two values based on whether the condition is true or false.

📘 Quick Intro

Written as condition ? trueResult : falseResult, the ternary operator is a concise way to make conditional assignments or return values without verbose if-else blocks.

🧠 Analogy

Think of a traffic signal. If it’s green, you drive; otherwise, you stop. Instead of writing out a big rulebook (if-else), the signal (condition) gives you an immediate answer (result). That’s the ternary operator—decision in a line.

🔧 Technical Explanation

  • Syntax: condition ? valueIfTrue : valueIfFalse
  • The condition must return a boolean result.
  • Both result expressions must return compatible types.
  • It is evaluated left to right and returns one of the two outcomes.
  • Nesting ternary operators is allowed but can harm readability.

🎯 Use Cases

  • Assigning values based on a simple condition.
  • Returning responses in single-line methods.
  • Reducing boilerplate if-else statements.
  • Displaying dynamic UI elements conditionally.

💻 Real Code Example

int score = 85;
string grade = score >= 90 ? "A" : "B";
Console.WriteLine(grade); // Output: B

bool isLoggedIn = true;
string message = isLoggedIn ? "Welcome back!" : "Please log in.";
Console.WriteLine(message); // Output: Welcome back!

❓ Interview Q&A

Q1: What is the ternary operator used for?
A: To return one of two values based on a condition, in a single line.

Q2: What is the syntax of the ternary operator?
A: condition ? valueIfTrue : valueIfFalse

Q3: Is it mandatory to use parentheses around ternary expressions?
A: Not mandatory but recommended for readability.

Q4: Can ternary be nested?
A: Yes, but it should be used with caution to maintain readability.

Q5: What happens if condition is false?
A: The right-hand value (after :) is returned.

Q6: Is it functionally equivalent to if-else?
A: Yes, for returning values or expressions—not for statements.

Q7: Can we use different data types on both sides of the ternary operator?
A: No, they must be of compatible types.

Q8: Can it be used for method return?
A: Yes, especially in short conditional returns.

Q9: Does it impact performance?
A: Negligible—performance is similar to if-else.

Q10: Where should it be avoided?
A: When logic becomes complex or hard to read—use if-else instead.

📝 MCQs

Q1. What symbol is used for ternary operator in C#?

  • ::
  • ?:
  • ??
  • &&

Q2. Which is the correct syntax?

  • condition ?? true : false
  • condition : true ? false
  • condition ? trueResult : falseResult
  • condition if true else false

Q3. What type should the condition return?

  • string
  • int
  • bool
  • object

Q4. Is nesting ternary operators good practice?

  • Always
  • Yes for speed
  • Only for UI
  • Not recommended

Q5. What is a common use of ternary operator?

  • Loop through array
  • Assign value based on condition
  • Log errors
  • Define class

Q6. Can you return different types in true and false parts?

  • Yes
  • Only with override
  • No
  • Sometimes

Q7. What happens if the condition is false?

  • Left-hand value
  • Error
  • Null
  • Right-hand value is returned

Q8. Which operator works similarly for value fallback?

  • ::
  • ==
  • &&
  • ??

Q9. Is ternary operator available in all C# versions?

  • Only C# 3+
  • No
  • Yes, since C# 1.0
  • Only C# 8+

Q10. What is returned: true ? 1 : 2;

  • 1
  • 2
  • true
  • false

💡 Bonus Insight

The ternary operator promotes clean, expressive code when used wisely. In modern C#, it can be combined with null-conditional and null-coalescing logic to write safe and compact expressions.

📄 PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

🔁 Navigation

💬 Feedback
🚀 Start Learning
Share:

Tags: