A nullable type is an extension of value types that allows these types to represent the value null
, in addition to their normal values. This is useful in scenarios where a value may be optional or not always available.
In C#, the value null
is used to represent an absence of value or reference. However, value types (such as int
, double
, bool
) cannot be null
by default, as they always have a default value.
To address this limitation, C# introduces nullable types, allowing value types to have the value null
.
Declaration and Use of Nullable Types
To declare a nullable type, the ?
operator is used after the value type. For example, int?
is a nullable type that can contain an integer or null
.
The syntax for declaring a nullable type is straightforward. For example,
int? age = null;
double? salary = 4500.50;
bool? isActive = null;
Here, number
is a variable that can contain an integer or the value null
.
Nullable Types and the Nullable<T>
Structure
Under the hood, the above syntax uses the generic structure Nullable<T>
. For example:
Nullable<int> myNumber = null;
Both forms (Nullable<T>
and T?
) are equivalent, but using the ?
operator is more common and simpler to read.
Nullability Check
To check if a nullable type has a value or is null, you can use the HasValue
and Value
properties. The HasValue
property indicates whether the nullable type contains a value:
if (age.HasValue)
{
Console.WriteLine($"The age is {age.Value}");
}
else
{
Console.WriteLine("The age is not assigned.");
}
Default Value Assignment
The null-coalescing operator ??
provides a compact way to handle nullable values, allowing you to define a default value in case the nullable is null
.
int? number = null;
int result = number ?? 5;
Console.WriteLine(result); // Prints 5
In this case, if number
is null
, result
will take the value 5. If number
has a value, result
will equal number
.
Conversion between Nullable and Non-Nullable Types
If you need to convert a nullable type to a non-nullable type, you can use the Value
property:
int nonNullableAge = age.Value; // Ensure age is not null before accessing Value.
Nullable Types and Operations
When working with nullable types, it’s important to understand how they behave in arithmetic and logical operations.
Arithmetic Operations
Arithmetic operations with nullable types follow specific rules: if either operand is null
, the result is also null
.
int? a = 10;
int? b = null;
int? sum = a + b;
Console.WriteLine(sum.HasValue ? sum.Value.ToString() : "null"); // Prints null
Here, because b
is null
, the sum results in null
.
Comparisons
Comparisons with nullable types are possible, but the result can be null
if either operand is null
.
int? x = 10;
int? y = null;
bool? isGreater = x > y;
Console.WriteLine(isGreater.HasValue ? isGreater.Value.ToString() : "null"); // Prints null
In this case, isGreater
is null
because y
is null
.
Improvements in C# 8.0 and Later Versions
Starting with C# 8.0, the ability to distinguish between nullable and non-nullable types in the language’s type system was introduced.
This means you can mark a type as “nullable” and the compiler will warn you if you try to assign a null value to a type that should not accept nulls.
#nullable enable
public class Person
{
public string Name { get; set; } // Cannot be null
public string? LastName { get; set; } // Can be null
}