C# Interview Question #84
What is the null-coalescing operator (??) in C#?
Advanced C# Language Features Mid-Level Intermediate
Quick Interview Answer
The null-coalescing operator returns its left operand when that value is not null; otherwise, it evaluates and returns the right operand.
Detailed Explanation
The null-coalescing operator returns its left operand when that value is not null; otherwise, it evaluates and returns the right operand.
It is commonly used to provide fallback values. The ??= operator assigns a value only when the target is currently null.
These operators reduce repetitive null-checking code and are frequently used with nullable reference and value types.
Code Example
string? name = null;
string displayName = name ?? "Guest";
List<string>? items = null;
items ??= new List<string>();