A Comprehensive Guide to Nullable Types in C#: Examples and Tutorial

Category > CSHARP || Published on : Tuesday, March 7, 2023 || Views: 251 || C# nullable types value types null values programming tutorial


Learn how to use nullable types in C# to represent value types that can also have null values. This tutorial covers step-by-step instructions and example codes to help you understand how to declare nullable types, access their values, assign values, and use the null-coalescing operator.

Introduction: Nullable types in C# are used to represent value types that can also have null values. In other words, nullable types can hold a value of their underlying value type, or they can be null. This feature was introduced in C# 2.0 to address the issue of value types not being able to store null values. In this article, we will discuss nullable types in C# with example codes.

Step by step guide to nullable types in C#:

Declare a nullable type variable: To declare a nullable type variable in C#, use the question mark (?) symbol after the data type. For example, to declare an integer variable that can hold null values, use the following syntax:

int? nullableInt = null;

In the above code, we have declared a nullable integer variable named nullableInt and initialized it to null.

Accessing the value of a nullable type: To access the value of a nullable type variable, use the Value property. The Value property throws an exception if the variable is null. To avoid exceptions, use the HasValue property to check if the variable has a value or not.

int? nullableInt = null;

if (nullableInt.HasValue)
{
    Console.WriteLine(nullableInt.Value);
}
else
{
    Console.WriteLine("nullableInt is null");
}

In the above code, we have used the HasValue property to check if the nullableInt variable has a value or not. If the variable has a value, we have printed its value using the Value property. Otherwise, we have printed a message indicating that the variable is null.

Assigning a value to a nullable type: To assign a value to a nullable type variable, use the = operator as you would for any other variable. If you want to assign null to a nullable type variable, use the keyword null.

int? nullableInt = null;

nullableInt = 10;

if (nullableInt.HasValue)
{
    Console.WriteLine(nullableInt.Value);
}
else
{
    Console.WriteLine("nullableInt is null");
}

In the above code, we have assigned the value 10 to the nullableInt variable. Since the variable has a value, the code will print its value.

Using the null-coalescing operator: The null-coalescing operator (??) is used to assign a default value to a nullable type variable if it is null.

int? nullableInt = null;

int notNullableInt = nullableInt ?? 0;

Console.WriteLine(notNullableInt);

In the above code, we have used the null-coalescing operator to assign a default value of 0 to the notNullableInt variable if the nullableInt variable is null.

Conclusion:

Nullable types in C# are useful when you need to represent value types that can also have null values. They can be declared using the question mark (?) symbol after the data type, and their value can be accessed using the Value property. To assign null to a nullable type variable, use the keyword null. The null-coalescing operator can be used to assign a default value to a nullable type variable if it is null.