What does the null-coalescing operator (??) do in C#?
It returns the left-hand operand if it is not null; otherwise, it returns the right-hand operand.
Click to reveal answer
beginner
How would you use the null-coalescing operator to provide a default value for a nullable variable?
Example: int? x = null; int y = x ?? 10; Here, y will be 10 because x is null.
Click to reveal answer
intermediate
Can the null-coalescing operator be chained? If yes, how?
Yes, you can chain it like a ?? b ?? c. It returns the first non-null value from left to right.
Click to reveal answer
intermediate
What is the difference between the null-coalescing operator (??) and the ternary operator when checking for null?
The null-coalescing operator is a concise way to check for null and provide a default, while the ternary operator requires a full condition like x != null ? x : defaultValue.
Click to reveal answer
beginner
Write a simple C# code snippet using the null-coalescing operator to assign a string variable a default value if it is null.
string name = null;
string displayName = name ?? "Guest";
// displayName will be "Guest" if name is null
Click to reveal answer
What will the expression int? a = null; int b = a ?? 5; assign to b?
A5
Bnull
C0
DCompilation error
✗ Incorrect
Since 'a' is null, the null-coalescing operator returns the right side value 5.
Which operator is used in C# to return the first non-null value between two expressions?
A??
B?:
C&&
D||
✗ Incorrect
The null-coalescing operator '??' returns the left operand if not null; otherwise, the right operand.
What will string s = null; var result = s ?? "default"; set result to?
ACompilation error
B"default"
CEmpty string
Dnull
✗ Incorrect
Since s is null, the operator returns the right side string "default".
Can the null-coalescing operator be used with non-nullable value types directly?
AYes, with any type
BOnly with integers
COnly with strings
DNo, only with nullable types or reference types
✗ Incorrect
It works with nullable types or reference types because they can be null.
What is the output of string? name = "Alice"; var display = name ?? "Guest";?
Anull
B"Guest"
C"Alice"
DCompilation error
✗ Incorrect
Since name is not null, the operator returns "Alice".
Explain how the null-coalescing operator (??) works in C# and give a simple example.
Think about providing a default value when something might be null.
You got /3 concepts.
Describe a scenario where chaining the null-coalescing operator is useful and how it works.
Imagine checking several options until you find one that is not null.
You got /3 concepts.
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
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 A
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
Step 1: Identify correct operator usage
The null-coalescing operator syntax is left ?? right, where right is the fallback value.
Step 2: Match syntax to code
Given string displayName = name ?? "Guest";, it correctly assigns "Guest" if name is null.
Final Answer:
name ?? "Guest" -> Option B
Quick Check:
Correct syntax uses ?? between variables [OK]
Hint: Remember: null-coalescing uses two question marks ?? [OK]