Nullable types
C#
int data type
nullable int
programming concepts

Nullableint vs. int? - Is there any difference?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In C#, Nullable<int> and int? are the same type. int? is just syntactic sugar for Nullable<int>. The real difference developers usually care about is not between those two spellings, but between int and a nullable integer type that can also represent null.

The Core Equivalence

These declarations are equivalent:

csharp
1Nullable<int> a = 5;
2int? b = 5;
3
4Console.WriteLine(a == b);

int? is simply the shorter and more common syntax.

This means:

  • both can store any valid int
  • both can store null
  • both expose HasValue and Value
  • both compile to the same underlying nullable value type concept

So if the question is literally Nullable<int> versus int?, there is no behavioral difference.

The Important Difference Is int Versus Nullable int

A plain int always has a value. It cannot be null.

csharp
int x = 0;

A nullable integer can represent either an integer or the absence of a value.

csharp
int? y = null;
y = 42;

That makes nullable integers useful when a number may be unknown, missing, or not applicable.

Common Operations

Nullable value types expose a few important members:

csharp
1int? count = null;
2
3Console.WriteLine(count.HasValue);
4Console.WriteLine(count.GetValueOrDefault());

HasValue tells you whether a real integer is present.

GetValueOrDefault() returns the stored value or the default for int, which is 0.

You can also use the null-coalescing operator:

csharp
int? count = null;
int safeCount = count ?? 0;
Console.WriteLine(safeCount);

That is often cleaner than reading .Value directly.

Why .Value Can Throw

If you access .Value when the nullable has no value, you get an exception.

csharp
int? amount = null;
// Console.WriteLine(amount.Value); // InvalidOperationException

So in normal code, prefer:

  • 'HasValue plus Value, or'
  • '??, or'
  • pattern matching

Example with pattern matching:

csharp
1int? amount = 10;
2
3if (amount is int actual)
4{
5    Console.WriteLine(actual);
6}

Lifting and Nullable Arithmetic

Operators on nullable value types are "lifted" in useful ways.

csharp
1int? a = 5;
2int? b = null;
3
4Console.WriteLine(a + 1);  // 6
5Console.WriteLine(a + b);  // null

If one operand is null, many arithmetic results become null. That is often exactly what you want for missing-data semantics.

Boxing and API Boundaries

Most everyday code does not need to think about this, but nullable value types behave differently from reference types at some boundaries such as boxing, reflection, or serialization.

For normal application logic, the important takeaway is still simple:

  • use int when a value must always exist
  • use int? when missing values are valid

The shorthand syntax does not change that design decision.

When Nullable Integers Are Useful

Common examples include:

  • database columns that may be null
  • optional filter parameters
  • values that are not yet computed
  • user-entered numeric fields that may be blank

In those cases, int? communicates intent better than inventing a magic sentinel value like -1.

Common Pitfalls

  • Thinking Nullable<int> and int? are different runtime types with different behavior.
  • Calling .Value without checking whether a value is present.
  • Using 0 as a fake "missing" value when null would be clearer.
  • Forgetting that nullable arithmetic often propagates null.
  • Confusing the real question of int versus int? with the spelling question of Nullable<int> versus int?.

Summary

  • 'Nullable<int> and int? are the same thing in C#.'
  • 'int? is just the shorter syntax and is usually preferred.'
  • The real design choice is between int and a nullable integer that can also be null.
  • Use HasValue, GetValueOrDefault(), ??, or pattern matching instead of unsafe .Value access.
  • Nullable integers are clearer than sentinel values when missing numeric data is a valid state.

Course illustration
Course illustration

All Rights Reserved.