Working with Nulls in C# Flashcards
What is a nullable type in C#?
Are instances of Nullable which represents the T value plus the “variation” of null.
What is the shorthand of Nullable?
T?
e.g: int?
What is the default value of the Nullable when not set?
null
What does the HasValue return when null?
false, if not null the return is true.
What does the Value method return, what happens when it is null?
The underlying value… exception when null
How does comparison of nullable types work?
As expected… no surprises.
What happens when you try to define a nullable int to a regular int?
compilation issue… you need to typecast or get is value.
What are the 3 null-checking operators? Do
1 - ? true : false
2 - ?? executed when null
3 - ?. and ?[
What is the null object pattern?
It is a software design pattern to reduce the ammount of null references.
Why using the null object pattern?
- Reduce the amount of runtime null exceptions.
- Reduce the amount of repetitive null checking. Therefore simplifying production code.
How to implement the null object pattern?
By implementing a dummy interface that does nothing.
What are the dangers of protect null code too much?
Can hide exceptions and make throubleshooting harder. Not used for error handling!!!