LearnThatStack Ace your next interview
C# · question
Question 9 of 112

What are nullable types and how do you use them?

beginner
← All C# questions
Re-explain

A nullable value type, written int?, lets a value type hold null as well as its normal values. It is shorthand for Nullable<int>, a struct with a HasValue flag and a Value property.

It fits data that can be missing, like a database column that allows NULL or an optional form field.

Check HasValue before reading Value, because reading Value when it holds nothing throws an InvalidOperationException. The ?? operator supplies a fallback instead, and GetValueOrDefault() returns the type's default.

int? nullableInt = null;
bool? nullableBool = null;
// Checking for null
if (nullableInt.HasValue)
{
    int value = nullableInt.Value;
}
// Null coalescing operator
int result = nullableInt ?? 0; // Use 0 if null

Do not confuse these with nullable reference types like string?, which are compile-time annotations only.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • If int? a is null, what do a + 1 and a > 5 return? Lifted operators. Arithmetic returns null, and <, >, <=, >= return false if either side is null. Use == null to test for missing.
  • What do you get when you box an int? that holds null? A null reference. A non-null int? boxes as a plain boxed int, not as Nullable<int>, so you can unbox it to int or int?.
  • How is int? different from string? at runtime? int? compiles to the Nullable<int> struct. string? is still a plain string, and the annotation only drives compiler warnings.
  • Why use int? for a missing value instead of a sentinel like -1 or 0? A sentinel can collide with real data, and every caller has to know about it. The compiler won't assign int? to int without a cast or a fallback.
  • With ADO.NET, how do you read a nullable database column into an int?? The reader returns DBNull.Value, not null. Check IsDBNull first, or let an ORM such as EF Core map the column to an int? property.

What you can say

  1. A nullable value type, written int?, lets a value type like int hold null too, so you can say a value is missing.
  2. It's shorthand for Nullable<int>, which is a struct with a HasValue flag and a Value property.
  3. I use it when data can be absent, like a database column that allows NULL or an optional form field.
  4. Before I read Value I check HasValue, because reading Value when there's nothing in it throws an InvalidOperationException.
  5. Often I skip the check and write nullableInt ?? 0 to get a fallback, or call GetValueOrDefault() to get the type's default.
  6. Don't confuse it with string?, a nullable reference type. That one is only a compile-time annotation, and the type stays string at runtime.

Weak answers to avoid

  • Reads .Value without checking Reading Value on an empty int? throws InvalidOperationException. Say you check HasValue first, or use ?? or GetValueOrDefault() instead.
  • Mixes up int? with string? string? is a compile-time annotation that only produces warnings. int? is a real Nullable<int> struct at runtime. State the difference before you're asked.
  • Gives the syntax with no use case Saying int? lets an int be null is only the definition. Add where it fits, like a nullable DB column or an optional field, and how you read it safely.
  • Calls int? a reference type Nullable<int> is a struct, a value type that holds a flag and a value. Calling it a reference type shows you don't know how it is stored.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the C# cheatsheet.

← Back to all C# questions
Pro · $10/mo

98 of 112 C# answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime