0Pricing
C# Academy · Lesson

Nullable Value Types

int? and the Nullable struct.

Why Nullable Value Types?

Value types like int, bool, and DateTime always hold a value. An int defaults to 0, never "no value".

Sometimes you need to represent missing data, such as an unanswered survey field or an empty database column. Nullable value types let a value type also hold null.

Declaring with int?

Add a ? after a value type to make it nullable. int? can hold any integer or null.

This is the most common way you will write nullable value types in everyday C# code.

int? age = null;
int count = 5;
age = count;
System.Console.WriteLine(age);

All lessons in this course

  1. Nullable Value Types
  2. Null-Conditional Operator
  3. Null-Coalescing Operators
  4. Guarding Against Nulls
← Back to C# Academy