By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
The Null-Conditional Operator (?.) and Null Coalescing (??) are essential C# features that simplify null-checking and provide default values. They enhance code readability and reduce the risk of NullReferenceException, a common runtime error. Incorrect usage can lead to bugs and performance issues. For example, missing a null check can crash an application, affecting user experience and reliability.
?.
string name = person?.Name;
⚠️ Pitfall: Overusing ?. can hide logic errors. Always verify if null is an expected state.
Apply Null Coalescing (??)
??
string name = person?.Name ?? "Unknown";
⚠️ Pitfall: Avoid using complex expressions on the right-hand side; keep it simple.
Chain Null-Conditional Operators
string address = person?.Address?.Street;
⚠️ Pitfall: Too much chaining can make code hard to read. Break it down if necessary.
Combine Null-Conditional and Null Coalescing
Experts view null-conditional and null coalescing as tools for writing clean, defensive code. They focus on making null-checks explicit and providing sensible defaults, treating null as a valid state rather than an error. This mindset leads to more robust and maintainable code.
Exam trap: Questions may trick you into ignoring null states.
The mistake: Overusing ?? with complex expressions.
Exam trap: Complex scenarios may tempt you to use nested ??.
The mistake: Chaining ?. excessively.
Exam trap: Deeply nested objects may lead to over-chaining.
The mistake: Ignoring side effects in null-coalescing.
Scenario: You have a Customer object that might be null. You need to safely access the customer's email.Question: How do you handle potential null values? Solution:1. Use ?. to check if Customer is null.2. Use ?? to provide a default email if Customer or Email is null.Answer: string email = customer?.Email ?? "[email protected]"; Why it works: The null-conditional operator safely accesses Email, and null coalescing provides a default value.
Customer
Email
string email = customer?.Email ?? "[email protected]";
Scenario: You have a nested object structure: Company.Department.Manager. You need to get the manager's name.Question: How do you safely access the manager's name? Solution:1. Chain ?. to access Manager.2. Use ?? to provide a default name if any part of the chain is null.Answer: string managerName = company?.Department?.Manager?.Name ?? "Unknown"; Why it works: Chaining ?. safely navigates the nested structure, and ?? provides a default.
Company.Department.Manager
Manager
string managerName = company?.Department?.Manager?.Name ?? "Unknown";
Scenario: You have a method that returns a nullable integer. You need to provide a default value if the method returns null.Question: How do you handle the nullable return value? Solution:1. Use ?? to provide a default value.Answer: int value = GetNullableInt() ?? 0; Why it works: Null coalescing provides a default value if GetNullableInt() returns null.
int value = GetNullableInt() ?? 0;
GetNullableInt()
result = expression?.Member ?? defaultValue;
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.