What is the Null-Coalescing Operator ?? in C#

πŸ’‘ Concept: Null-Coalescing Operator (??)

The null-coalescing operator ?? in C# provides a concise way to return a default value when a nullable value is null. It simplifies null checks and improves code readability.

πŸ“˜ Quick Intro

The ?? operator returns the left-hand operand if it is not null; otherwise, it returns the right-hand operand. It is widely used for fallback logic.

🧠 Analogy

Imagine ordering coffee. If your favorite blend is unavailable (null), the barista gives you a backup option (default). That’s exactly what the ?? operator doesβ€”fallback when your primary choice is null.

πŸ”§ Technical Explanation

  • Syntax: var result = value ?? defaultValue;
  • Only evaluates the right-hand side if the left-hand is null.
  • Works with nullable types and reference types.
  • Helps eliminate verbose null-checking logic.

🎯 Use Cases

  • Set fallback values for nullable variables.
  • Simplify property assignments when using user inputs.
  • Reduce if-else statements when checking for null.

πŸ’» Real Code Example

string userInput = null;
string result = userInput ?? "Default Value";
Console.WriteLine(result); // Output: Default Value

int? score = null;
int finalScore = score ?? 0;
Console.WriteLine(finalScore); // Output: 0

❓ Interview Q&A

Q1: What does the null-coalescing operator do?
A: It returns the left-hand operand if it’s not null; otherwise, it returns the right-hand operand.

Q2: What is the syntax for the null-coalescing operator?
A: var result = value ?? defaultValue;

Q3: Can it be used with value types?
A: Yes, if the value type is nullable (e.g., int?).

Q4: Does it short-circuit evaluation?
A: Yes, it only evaluates the right-hand if the left is null.

Q5: Is this operator available in all C# versions?
A: It’s available since C# 2.0.

Q6: Can you use it inside expressions or assignments?
A: Yes, it’s often used in assignments and method arguments.

Q7: What is the difference between ?? and ??: (null-coalescing assignment)?
A: ?? returns a fallback; ??: assigns if null.

Q8: Can it chain with multiple operands?
A: Yes: a ?? b ?? c returns the first non-null value.

Q9: What happens if both operands are null?
A: The result is null.

Q10: Does it throw exception on null left operand?
A: No, it handles it safely by evaluating the right-hand side.

πŸ“ MCQs

Q1. What is the null-coalescing operator in C#?

  • ?:
  • ??
  • ::
  • --

Q2. What does 'x ?? y' return if x is not null?

  • y
  • x
  • null
  • Exception

Q3. Which C# version introduced ?? operator?

  • C# 1.0
  • C# 2.0
  • C# 3.0
  • C# 5.0

Q4. Can ?? be used with nullable int?

  • No
  • Only with reference types
  • Yes
  • Only in C# 11

Q5. Which is correct usage of ??

  • input ? 'default';
  • if (input) then default;
  • result = input ?? 'default';
  • input =? default

Q6. Is the right operand of ?? always evaluated?

  • Always
  • Never
  • Only if left is null
  • Only in value types

Q7. What does ?? do with null left and right?

  • Throws error
  • Returns right
  • Returns null
  • Returns left

Q8. Can you chain multiple ???

  • No
  • Yes
  • Only twice
  • Not in expressions

Q9. What is the benefit of using ??

  • Faster loops
  • Cleaner null-check logic
  • Memory allocation
  • Boxing

Q10. What is difference between ?? and ??=

  • Same
  • ?? returns fallback, ??= assigns fallback
  • ??= is deprecated
  • None

πŸ’‘ Bonus Insight

The null-coalescing operator enhances code readability and safety by removing the need for explicit null checks. It’s essential for clean, defensive programming in modern C#.

πŸ“„ PDF Download

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

πŸ” Navigation

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: