Nullable Types
Vaibhav • September 11, 2025
In earlier articles, we learned that every variable in C# must have a type, and each type has a default value.
For example, an int
variable holds whole numbers and defaults to 0
if not initialized in a class field. But what if you want to represent the idea
of “no value” for a number? What if you want to say, “This number hasn’t been set yet”? That’s where
nullable types come in.
Nullable types allow certain variables to hold either a value or nothing at all - a concept known as null
. This is especially useful when working with data that might be missing,
optional, or unknown. In this article, we’ll explore how to declare nullable types, how they behave, and how to
use them safely - all using concepts we’ve already covered.
What is a nullable type?
A nullable type is a version of a value type (like int
, bool
, or double
) that can also hold null
. Normally, value types cannot be null - they must always contain a valid
number, true/false, or similar. But by adding a question mark (?
) after the
type name, you tell C# that this variable is allowed to be empty.
int? age = null;
Console.WriteLine(age);
In this example, age
is declared as a nullable integer. It doesn’t hold a
number - it holds null
, which means “no value.” This is different from 0
, which is a valid number. null
means the value
hasn’t been set yet.
You can make any value type nullable by adding ?
after the type name. For example: bool?
, double?
, char?
. Reference types like string
are already nullable by default.
Assigning and clearing values
You can assign a value to a nullable variable just like any other variable. You can also clear it by setting it
to null
.
int? temperature = 22;
Console.WriteLine(temperature); // Output: 22
temperature = null;
Console.WriteLine(temperature); // Output: (blank)
This shows how a nullable variable can switch between having a value and being empty. This is useful when you want to reset a value or mark it as unknown.
Using the value inside a nullable
When a nullable variable has a value, you can copy it into a regular variable and use it normally. For example:
int? points = 100;
int actualPoints = points.Value;
Console.WriteLine(actualPoints);
Here, points
contains 100
, so we copy it into
a normal int
called actualPoints
. Now we can
use actualPoints
like any other number.
You should only use .Value
if you are sure the
variable is not null. If the variable is null, trying to access .Value
will
cause an error.
Nullable types in messages
You can use nullable variables in string interpolation to build dynamic messages. For example:
int? age = 30;
Console.WriteLine($"User age: {age}");
If the variable has a value, it appears in the message. If it is null
, the
message simply shows a blank space.
Nullable types in algorithms
In Chapter 1, we introduced algorithms as step-by-step instructions. Nullable types often play a role in these steps - especially when dealing with optional inputs or incomplete data. For example:
int? age = 25;
int ageInDays = age.Value * 365;
Console.WriteLine($"You are approximately {ageInDays} days old.");
This calculation only works if age
is not null. In real-world programs, you
must be careful to avoid using .Value
on a null variable.
Common mistakes and how to avoid them
One common mistake is trying to use a nullable variable when it has no value. For example:
int? score = null;
int finalScore = score.Value; // ❌ Error if score is null
This will cause your program to crash because score
is empty. The correct
approach is to make sure the variable has been given a value before using .Value
.
Use nullable types when a value might be missing. Always ensure a value
exists before using .Value
. This makes your code safer and easier to
understand.
Summary
Nullable types allow value-type variables to hold either a value or null
. You
learned how to declare a nullable using int?
, assign and clear values, and
safely access the value using .Value
. Nullable types are useful for
representing optional or missing data, and they help your programs handle real-world uncertainty.
As you continue learning C#, you’ll use nullable types in forms, databases, user input, and more. They’re a simple but powerful tool that makes your code more flexible and reliable.