Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Null-coalescing operator in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Null-coalescing operator
Evaluate left expression
↓
Is left null?
No→Use left value
Yes↓
Use right expression value
The null-coalescing operator checks if the left value is null; if not, it uses it, otherwise it uses the right value.
Execution Sample
C Sharp (C#)
string name = null;
string displayName = name ?? "Guest";
Console.WriteLine(displayName);
This code assigns "Guest" to displayName if name is null, then prints displayName.
Execution Table
StepExpression EvaluatedValueIs Null?Result Used
1namenullYesEvaluate right expression
2"Guest""Guest"-Use "Guest"
3displayName assigned"Guest"--
4Console.WriteLine(displayName)"Guest"-Output: Guest
💡 Left expression is null, so right expression "Guest" is used.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
namenullnullnullnull
displayNameunassignedunassigned"Guest""Guest"
Key Moments - 2 Insights
Why does displayName get "Guest" instead of null?
Because in step 1 the left expression 'name' is null, so the operator uses the right expression "Guest" as shown in execution_table row 2.
What happens if name is not null?
If name is not null, the operator uses the left value directly, skipping the right expression, as shown in the flow diagram and execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'displayName' after step 2?
A"Guest"
Bnull
C"name"
Dunassigned
💡 Hint
Check the 'Result Used' and 'Value' columns at step 2 in the execution_table.
At which step does the operator decide to use the right expression?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Is Null?' column in execution_table row 1.
If 'name' was "Alice" instead of null, what would 'displayName' be after step 3?
A"Guest"
B"Alice"
Cnull
Dunassigned
💡 Hint
Refer to the concept_flow and key_moments about using left value when not null.
Concept Snapshot
Null-coalescing operator (??) returns the left value if not null; otherwise, it returns the right value.
Syntax: var result = left ?? right;
Use it to provide default values when a variable might be null.
It evaluates right only if left is null.
Commonly used to avoid null checks.
Full Transcript
The null-coalescing operator in C# checks if the left expression is null. If it is not null, it uses the left value. If it is null, it uses the right expression's value instead. For example, if a variable 'name' is null, using 'name ?? "Guest"' will result in "Guest". This operator helps provide default values easily without writing if-else checks. The execution table shows step-by-step how the operator evaluates the left expression, finds it null, then uses the right expression. Variables change accordingly, with 'displayName' assigned the default "Guest". If 'name' was not null, the operator would use 'name' directly. This operator is a simple way to handle null values safely and clearly.

Practice

(1/5)
1.

What does the null-coalescing operator ?? do in C#?

easy
A. Returns the left value if it is not null; otherwise, returns the right value.
B. Checks if two values are equal.
C. Assigns a value only if the variable is null.
D. Throws an exception if the left value is null.

Solution

  1. 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.
  2. Step 2: Analyze behavior

    If the left value is not null, it returns that value; otherwise, it returns the right value.
  3. Final Answer:

    Returns the left value if it is not null; otherwise, returns the right value. -> Option A
  4. Quick Check:

    Null-coalescing operator = fallback value [OK]
Hint: Think: "Use right value only if left is null" [OK]
Common Mistakes:
  • Confusing it with equality check
  • Thinking it assigns values automatically
  • Assuming it throws exceptions on null
2.

Which of the following is the correct syntax to use the null-coalescing operator in C#?

string name = null;
string displayName = ??? "Guest";
easy
A. name ??
B. name ?? "Guest"
C. name ? "Guest"
D. name ?: "Guest"

Solution

  1. Step 1: Identify correct operator usage

    The null-coalescing operator syntax is left ?? right, where right is the fallback value.
  2. Step 2: Match syntax to code

    Given string displayName = name ?? "Guest";, it correctly assigns "Guest" if name is null.
  3. Final Answer:

    name ?? "Guest" -> Option B
  4. Quick Check:

    Correct syntax uses ?? between variables [OK]
Hint: Remember: null-coalescing uses two question marks ?? [OK]
Common Mistakes:
  • Using single question mark ? instead of ??
  • Placing ?? after the variable without right value
  • Confusing with ternary operator syntax
3.

What will be the output of the following C# code?

string input = null;
string result = input ?? "Default";
Console.WriteLine(result);
medium
A. null
B. input
C. Default
D. Compilation error

Solution

  1. Step 1: Check the value of input

    The variable input is assigned null.
  2. Step 2: Apply null-coalescing operator

    The expression input ?? "Default" returns "Default" because input is null.
  3. Final Answer:

    Default -> Option C
  4. Quick Check:

    Null input returns fallback string [OK]
Hint: If left is null, output right value [OK]
Common Mistakes:
  • Expecting output to be 'null' string
  • Confusing with ternary operator output
  • Thinking it causes compile error
4.

Identify the error in the following code snippet using the null-coalescing operator:

int? number = null;
int result = number ?? null;
Console.WriteLine(result);
medium
A. Variable 'number' must be non-nullable.
B. The null-coalescing operator is used incorrectly; should be &&.
C. No error; code runs fine and prints 0.
D. Cannot use null as right operand for int result.

Solution

  1. Step 1: Analyze variable types

    number is nullable int (int?), but result is non-nullable int.
  2. Step 2: Check right operand of null-coalescing

    The right operand is null, which cannot be assigned to non-nullable int result.
  3. Final Answer:

    Cannot use null as right operand for int result. -> Option D
  4. Quick Check:

    Right side must be non-null for non-nullable target [OK]
Hint: Right side of ?? must match target type, not null [OK]
Common Mistakes:
  • Using null as fallback for non-nullable variable
  • Confusing ?? with logical operators
  • Ignoring nullable type rules
5.

Given the following code, what will be the output?

string? firstName = null;
string? lastName = "Smith";
string fullName = firstName ?? lastName ?? "Unknown";
Console.WriteLine(fullName);
hard
A. Smith
B. null
C. Unknown
D. Compilation error

Solution

  1. Step 1: Evaluate first null-coalescing operation

    firstName is null, so firstName ?? lastName evaluates to lastName, which is "Smith".
  2. Step 2: Evaluate second null-coalescing operation

    Since lastName is not null, the second ?? "Unknown" is not used.
  3. Final Answer:

    Smith -> Option A
  4. Quick Check:

    Chained ?? returns first non-null value [OK]
Hint: Chain ?? to pick first non-null value [OK]
Common Mistakes:
  • Expecting 'Unknown' when lastName is not null
  • Thinking null propagates through chain
  • Assuming compilation error with nullable strings
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard