The null-conditional operator is ?. in C#. It allows safe access to members or elements of an object that might be null, preventing exceptions by returning null instead.
Click to reveal answer
beginner
How does ?. help prevent exceptions?
When you use ?., if the object before it is null, the whole expression returns null instead of throwing a NullReferenceException. This makes your code safer and cleaner.
Click to reveal answer
beginner
Example: What does person?.Name do if person is null?
If person is null, person?.Name returns null instead of throwing an error. If person is not null, it returns the Name property.
Click to reveal answer
intermediate
Can the null-conditional operator be used with methods? Give an example.
Yes, you can call a method safely using ?.. For example, person?.GetAge() calls GetAge() only if person is not null. Otherwise, it returns null.
Click to reveal answer
intermediate
What happens when you use the null-conditional operator with an event, like MyEvent?.Invoke()?
Using ?. with events safely invokes the event only if it has subscribers (is not null). This prevents exceptions when no handlers are attached.
Click to reveal answer
What does the null-conditional operator ?. do in C#?
ASafely accesses members of an object that might be null
BThrows an exception if the object is null
CAssigns null to an object
DChecks if an object is not null
✗ Incorrect
The null-conditional operator ?. safely accesses members or methods of an object that might be null, returning null instead of throwing an exception.
What will person?.Name return if person is null?
ADefault value of Name's type
BEmpty string
CThrows NullReferenceException
Dnull
✗ Incorrect
If person is null, person?.Name returns null safely without throwing an exception.
Which of these is a valid use of the null-conditional operator?
Aperson?.GetAge()
Bperson.GetAge()?
Cperson?GetAge()
Dperson.GetAge?.()
✗ Incorrect
person?.GetAge() safely calls the method only if person is not null.
How does MyEvent?.Invoke() help when raising events?
AIt always invokes the event
BIt unsubscribes all handlers
CIt invokes the event only if there are subscribers
DIt throws an exception if no subscribers
✗ Incorrect
Using ?. with events invokes the event only if it has subscribers, preventing exceptions.
What type of value does the null-conditional operator return when the object is null?
ADefault value of the member
Bnull
CEmpty string
DZero
✗ Incorrect
When the object is null, the null-conditional operator returns null instead of throwing an error.
Explain how the null-conditional operator ?. works and why it is useful in C#.
Think about how it helps avoid errors when objects might be missing.
You got /4 concepts.
Describe a scenario where using ?. with an event is beneficial.
Consider what happens if you try to raise an event with no listeners.
You got /3 concepts.
Practice
(1/5)
1. What does the null-conditional operator ?. do in C#?
easy
A. It safely accesses a member only if the object is not null, otherwise returns null.
B. It throws an exception if the object is null.
C. It converts a null object to an empty string.
D. It forces the object to be non-null.
Solution
Step 1: Understand the purpose of the null-conditional operator
The operator ?. is used to access members of an object only if the object is not null.
Step 2: Behavior when object is null
If the object is null, instead of throwing an exception, it returns null safely.
Final Answer:
It safely accesses a member only if the object is not null, otherwise returns null. -> Option A
Quick Check:
Null-conditional operator = safe member access [OK]
Hint: Remember: ?. returns null instead of error if object is null [OK]
Common Mistakes:
Thinking it throws an exception on null
Confusing it with null-coalescing operator
Assuming it converts null to empty string
2. Which of the following is the correct syntax to safely access the Length property of a string variable text that might be null?
easy
A. int length = text.Length?;
B. int? length = text?.Length;
C. int length = text?.Length;
D. int? length = text.Length?.;
Solution
Step 1: Identify correct null-conditional syntax
The null-conditional operator is ?. placed before the member name, like text?.Length.
Step 2: Match variable type with possible null
Since text might be null, the result text?.Length is nullable, so use int? to hold it.
Final Answer:
int? length = text?.Length; -> Option B
Quick Check:
Correct syntax = int? length = text?.Length; [OK]
Hint: Use int? for nullable results with ?. operator [OK]
Step 1: Evaluate null-conditional operator on null string
The variable name is null, so name?.Length returns null safely.
Step 2: Check output logic
The code prints "Null" if length is null, otherwise prints the length. Since length is null, it prints "Null".
Final Answer:
Null -> Option A
Quick Check:
Null string length = null, prints "Null" [OK]
Hint: ?. returns null for null object, so output is "Null" [OK]
Common Mistakes:
Expecting 0 instead of null
Thinking it throws exception
Confusing null with empty string
4. Identify the error in this code snippet:
Person? person = null;
int age = person?.Age;
Console.WriteLine(age);
medium
A. Missing semicolon after Console.WriteLine.
B. Null-conditional operator cannot be used on objects.
C. Cannot assign nullable int to non-nullable int without cast.
D. Person class must be non-nullable.
Solution
Step 1: Analyze the type of person?.Age
The expression person?.Age returns int? (nullable int) because person might be null.
Step 2: Check assignment compatibility
Variable age is declared as int (non-nullable), so assigning int? to int without conversion causes a compile error.
Final Answer:
Cannot assign nullable int to non-nullable int without cast. -> Option C
Quick Check:
Nullable to non-nullable assignment needs cast [OK]
Hint: Nullable result needs nullable variable or cast [OK]
Common Mistakes:
Ignoring nullable type returned by ?. operator
Assuming no error on assignment
Thinking null-conditional operator is invalid here
5. Given a list of Person objects where some may be null, how can you safely get a List<int> of their ages (with 0 for null persons) using the null-conditional operator?
List<Person?> people = new() { new Person(30), null, new Person(25) };
var ages = ???;
Choose the correct code to replace ???.
hard
A. people.Select(p => p?.Age).ToList();
B. people.Select(p => p.Age).ToList();
C. people.Select(p => p.Age ?? 0).ToList();
D. people.Select(p => p?.Age ?? 0).ToList();
Solution
Step 1: Understand the list and nulls
The list contains Person? objects, some are null, so accessing Age needs safe access.
Step 2: Use null-conditional with fallback
p?.Age safely accesses Age or returns null; using ?? 0 replaces null ages with 0 to avoid nullable list.
Step 3: Check other options
people.Select(p => p?.Age).ToList(); returns nullable ints, people.Select(p => p.Age).ToList(); throws exception on null, people.Select(p => p.Age ?? 0).ToList(); is invalid because p can be null.
Final Answer:
people.Select(p => p?.Age ?? 0).ToList(); -> Option D