Working with Nulls in C# Flashcards

1
Q

What is a nullable type in C#?

A

Are instances of Nullable which represents the T value plus the “variation” of null.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the shorthand of Nullable?

A

T?

e.g: int?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the default value of the Nullable when not set?

A

null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the HasValue return when null?

A

false, if not null the return is true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the Value method return, what happens when it is null?

A

The underlying value… exception when null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does comparison of nullable types work?

A

As expected… no surprises.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happens when you try to define a nullable int to a regular int?

A

compilation issue… you need to typecast or get is value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the 3 null-checking operators? Do

A

1 - ? true : false
2 - ?? executed when null
3 - ?. and ?[

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the null object pattern?

A

It is a software design pattern to reduce the ammount of null references.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why using the null object pattern?

A
  • Reduce the amount of runtime null exceptions.

- Reduce the amount of repetitive null checking. Therefore simplifying production code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to implement the null object pattern?

A

By implementing a dummy interface that does nothing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the dangers of protect null code too much?

A

Can hide exceptions and make throubleshooting harder. Not used for error handling!!!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly