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

Learn More About C# πŸ“š

1. What is C#? πŸ‘‰ Explained
2. Main Features of C# πŸ‘‰ Explained
3. Difference Between C# and Java πŸ‘‰ Explained
4. Common Language Runtime (CLR) in C# πŸ‘‰ Explained
5. Common Type System (CTS) in C# πŸ‘‰ Explained
6. Common Language Specification (CLS) in C# πŸ‘‰ Explained
7. Value Types vs Reference Types in C# πŸ‘‰ Explained
8. What is a Namespace in C#? πŸ‘‰ Explained
9. Purpose of the 'using' Keyword in C# πŸ‘‰ Explained
10. Different Data Types in C# πŸ‘‰ Explained
11. Difference Between int and Int32 in C# πŸ‘‰ Explained
12. Difference Between float, double, and decimal in C# πŸ‘‰ Explained
13. What is the Default Value of a Boolean in C#? πŸ‘‰ Explained
14. What is Boxing and Unboxing in C# πŸ‘‰ Explained
15. What are the Different Types of Operators in C# πŸ‘‰ Explained
16. Difference Between Equals and == in C# πŸ‘‰ Explained
17. What is the Null-Coalescing Operator ?? in C# πŸ‘‰ Explained
18. What is the Ternary Operator in C# πŸ‘‰ Explained
19. How Does the Switch Statement Work in C# πŸ‘‰ Explained
20. What is Object-Oriented Programming in C# πŸ‘‰ Explained
21. What are the Four Pillars of OOP in C# πŸ‘‰ Explained
22. What is Encapsulation in C# πŸ‘‰ Explained
23. What is Inheritance in C# πŸ‘‰ Explained
24. What is Polymorphism in C# πŸ‘‰ Explained
25. What is Abstraction in C# πŸ‘‰ Explained
26. What is an Abstract Class in C# πŸ‘‰ Explained
27. What is an Interface in C# πŸ‘‰ Explained
28. Can a Class Implement Multiple Interfaces in C#? πŸ‘‰ Explained
29. Difference Between Abstract Class and Interface in C# πŸ‘‰ Explained
30. How Do You Create a Class in C#? πŸ‘‰ Explained
31. What is a Constructor in C# πŸ‘‰ Explained
32. What Are the Types of Constructors in C# πŸ‘‰ Explained
33. What is a Static Constructor in C# πŸ‘‰ Explained
34. Difference Between Static and Non-Static Members in C# πŸ‘‰ Explained
35. What is the Use of 'this' Keyword in C# πŸ‘‰ Explained
36. What is a Destructor in C# πŸ‘‰ Explained
37. What is Object Initializer Syntax in C# πŸ‘‰ Explained
38. What is the Difference Between Field and Property in C# πŸ‘‰ Explained
Share:

Tags:


Feedback Modal Popup