0
0
C Sharp (C#)programming~5 mins

Nullable value types in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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#?
Adouble!
Bnullable double
Cdouble?
Ddouble*
What does int? x = null; mean?
Ax is a string
Bx is a nullable reference type
Cx is a non-nullable integer
Dx is an integer that can be null
Which property tells if a nullable type has a value?
AIsEmpty
BHasValue
CValue
DIsNull
What does the expression myInt ?? 10 do?
AReturns myInt if not null, otherwise 10
BAlways returns 10
CThrows an error if myInt is null
DReturns null if myInt is null
What happens if you access Value on a null nullable type?
AThrows InvalidOperationException
BReturns 0
CReturns null
DReturns default value of the type
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.