Recall & Review
beginner
What is the null-conditional operator in C#?
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#?✗ 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?✗ 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?
✗ Incorrect
person?.GetAge() safely calls the method only if person is not null.How does
MyEvent?.Invoke() help when raising events?✗ 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?
✗ 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.