What if you could replace long null checks with a tiny symbol that does it all?
Why Null-coalescing operator in C Sharp (C#)? - Purpose & Use Cases
Imagine you have a program that asks for a user's nickname, but sometimes the user doesn't provide one. You want to show a default name instead. Doing this by checking if the nickname is null every time can get messy and long.
Manually checking if a value is null before using it means writing extra lines of code repeatedly. This makes the code longer, harder to read, and easy to forget, which can cause bugs or crashes when null values sneak in.
The null-coalescing operator lets you quickly say: "If this value is null, use this default instead." It makes your code shorter, cleaner, and safer by handling nulls in one simple step.
string name = nickname != null ? nickname : "Guest";string name = nickname ?? "Guest";It enables writing clear and concise code that safely handles missing or null values without extra checks.
When showing a user's profile, if their display name is missing, you can quickly show "Anonymous" instead without complicated if-statements.
Manually checking for null is repetitive and error-prone.
The null-coalescing operator simplifies choosing defaults for null values.
It makes code easier to read and less buggy.
Practice
What does the null-coalescing operator ?? do in C#?
Solution
Step 1: Understand the operator's purpose
The null-coalescing operator??is used to provide a fallback value when the left-hand side is null.Step 2: Analyze behavior
If the left value is not null, it returns that value; otherwise, it returns the right value.Final Answer:
Returns the left value if it is not null; otherwise, returns the right value. -> Option AQuick Check:
Null-coalescing operator = fallback value [OK]
- Confusing it with equality check
- Thinking it assigns values automatically
- Assuming it throws exceptions on null
Which of the following is the correct syntax to use the null-coalescing operator in C#?
string name = null; string displayName = ??? "Guest";
Solution
Step 1: Identify correct operator usage
The null-coalescing operator syntax isleft ?? right, where right is the fallback value.Step 2: Match syntax to code
Givenstring displayName = name ?? "Guest";, it correctly assigns "Guest" if name is null.Final Answer:
name ?? "Guest" -> Option BQuick Check:
Correct syntax uses ?? between variables [OK]
- Using single question mark ? instead of ??
- Placing ?? after the variable without right value
- Confusing with ternary operator syntax
What will be the output of the following C# code?
string input = null; string result = input ?? "Default"; Console.WriteLine(result);
Solution
Step 1: Check the value of
The variableinputinputis assignednull.Step 2: Apply null-coalescing operator
The expressioninput ?? "Default"returns "Default" becauseinputis null.Final Answer:
Default -> Option CQuick Check:
Null input returns fallback string [OK]
- Expecting output to be 'null' string
- Confusing with ternary operator output
- Thinking it causes compile error
Identify the error in the following code snippet using the null-coalescing operator:
int? number = null; int result = number ?? null; Console.WriteLine(result);
Solution
Step 1: Analyze variable types
numberis nullable int (int?), butresultis non-nullable int.Step 2: Check right operand of null-coalescing
The right operand isnull, which cannot be assigned to non-nullable intresult.Final Answer:
Cannot use null as right operand for int result. -> Option DQuick Check:
Right side must be non-null for non-nullable target [OK]
- Using null as fallback for non-nullable variable
- Confusing ?? with logical operators
- Ignoring nullable type rules
Given the following code, what will be the output?
string? firstName = null; string? lastName = "Smith"; string fullName = firstName ?? lastName ?? "Unknown"; Console.WriteLine(fullName);
Solution
Step 1: Evaluate first null-coalescing operation
firstNameis null, sofirstName ?? lastNameevaluates tolastName, which is "Smith".Step 2: Evaluate second null-coalescing operation
SincelastNameis not null, the second?? "Unknown"is not used.Final Answer:
Smith -> Option AQuick Check:
Chained ?? returns first non-null value [OK]
- Expecting 'Unknown' when lastName is not null
- Thinking null propagates through chain
- Assuming compilation error with nullable strings
Ready to prove it?
Pass the test and this pattern moves unknown
