Recall & Review
beginner
What is a nullable value type in C#?
A nullable value type is a value type that can also hold a null value, allowing it to represent the absence of a value. It is declared using a question mark after the type, for example,
int?.Click to reveal answer
beginner
How do you declare a nullable integer in C#?
You declare a nullable integer by adding a question mark after the type:
int? myNumber;. This means myNumber can hold any integer or null.Click to reveal answer
intermediate
What does the
HasValue property do in nullable types?The
HasValue property returns true if the nullable type contains a non-null value, and false if it is null.Click to reveal answer
intermediate
How can you safely get the value of a nullable type?
You can use the
Value property if HasValue is true, or use the null-coalescing operator ?? to provide a default value, for example: int value = myNumber ?? 0;.Click to reveal answer
intermediate
What happens if you try to access the
Value property of a nullable type that is null?Accessing the
Value property when the nullable type is null throws an InvalidOperationException. Always check HasValue before accessing Value.Click to reveal answer
How do you declare a nullable double in C#?
✗ Incorrect
In C#, you declare a nullable value type by adding a question mark after the type, like
double?.What does
int? x = null; mean?✗ Incorrect
The
int? means x is a nullable integer, so it can hold an integer or null.Which property tells if a nullable type has a value?
✗ Incorrect
The
HasValue property returns true if the nullable type contains a value.What does the expression
myInt ?? 10 do?✗ Incorrect
The null-coalescing operator
?? returns the left value if it is not null, otherwise the right value.What happens if you access
Value on a null nullable type?✗ Incorrect
Accessing
Value when the nullable type is null throws an InvalidOperationException.Explain what nullable value types are and why they are useful in C#.
Think about how you might represent an empty or missing number.
You got /4 concepts.
Describe how to safely work with nullable value types to avoid exceptions.
Consider how to avoid errors when the value might be missing.
You got /3 concepts.