C# Nullable Value Type

Summary: in this tutorial, you’ll learn about C# Nullable Value type and how to use it effectively.

Introduction to the C# Nullable Value Type

The following declares a variable isCompleted with the bool type and initializes its value to false:

using static System.Console;

bool isCompleted = false;
WriteLine(isCompleted);Code language: C# (cs)

Since the isCompleted variable has type bool, you can assign either true or false to it:

using static System.Console;

bool isCompleted = false ;
isCompleted = true;

WriteLine(isCompleted);Code language: C# (cs)

But things are getting more complicated when you work with external data, such as the result of an API call or the result set from a database.

If the data comes from the database is NULL, not true or false, then you won’t be able to assign it to the isCompleted variable. If you attempt to do so, you’ll get an error like this:

using static System.Console;

bool isCompleted = false ;

// Error: Cannot convert null to 'bool'
// because it is a non-nullable value type
isCompleted = null;  // null comes from DB

WriteLine(isCompleted);Code language: C# (cs)

This problem occurs not only in the bool type but also in other types like int, string, decimal, etc.

To solve this issue, C# introduces the nullable value type. Notice that we’re also having a nullable reference type that will be covered later.

A nullable value type T? represents all values of the underlying value type T and an additional null value. The default value of a nullable value type T? is null.

For example, bool? variable can accept one of three values: true, false, or null:

using static System.Console;

bool? isCompleted = false ;
isCompleted = null;  // OK

WriteLine(isCompleted);Code language: C# (cs)

To make a value type (T) nullable, you can append a question mark (?) to the type as follows:

T?Code language: C# (cs)

For example:

int?, decimal?Code language: C# (cs)

Technically, any nullable value type is an instance of the generic System.Nullable<T>. Therefore, the following are equivalent:

bool? isCompleted;Code language: C# (cs)

And

Nullable<bool> isCompleted;Code language: C# (cs)

In practice, you’ll rarely use the Nullable<T> type. Instead, you’ll use T? because it’s more concise.

C# Nullable Value Type properties

The Nullable<T>.HasValue returns true if the object has a value of its underlying type (T) or false otherwise. For example:

using static System.Console;

int? count = null;
WriteLine(count.HasValue); // False

count = 1;
WriteLine(count.HasValue); // TrueCode language: C# (cs)

In this example, the count.HasValue returns false because the count is null initially. But after assigning it an integer (1), its HasValue returns true.

If the HasValue property returns true, you can get the value of the underlying type using the Nullable<T>.Value property:

using static System.Console;

int? count = 1;
if (count.HasValue)
{
    WriteLine(count.Value); // 1
}Code language: C# (cs)

In this example, the count variable has the value 1 therefore the HasValue property returns true and Value returns 1.

If the HasValue returns false and you access the Value property, you’ll get an InvalidOperationException exception.

Likewise, you can compare an object of the nullable value type with null rather than using the HasValue property as shown in the following example:

using static System.Console;

int? count = 1;
if (count is not null)
{
    WriteLine(count.Value); // 1
}Code language: C# (cs)

Summary

  • Nullable<T> can hold values of the underlying value T and additional null value.
  • Use the Nullable<T>.HasValue property to check if an instance of the Nullable<T> contains a value of the underlying type T.
  • Use Nullable<T>.Value property to access the value of the instance of Nullable<T> when the Nullable<T>.HasValue property is true.
Was this tutorial helpful ?