What is nullable int in C#?

A nullable type is a data type that can represent the normal range of values for its underlying data type plus an additional special value that indicates the absence of a value. In other words, it allows a variable to be assigned either a valid value of its underlying type or a special marker for “no value,” often denoted as null.

Here are five specific applications of nullable types:

Applications
Applications

  1. Database fields: Representing optional or nullable fields in a database where certain data may be missing.

  2. User input: Handling optional input fields in forms or user interfaces where values are not always provided.

  3. Configuration settings: Managing optional configuration parameters where a null value signifies the use of a default setting.

  4. Mathematical calculations: Indicating the result of a calculation that might not always have a valid or meaningful value.

  5. APIs and services: Designing APIs or services where certain parameters or properties are optional, and a null value represents their absence.

In C#, nullable types are represented by appending a ? to the underlying value type. For example: int?and double?. int? is pronounced as int nullable. It allows the variable to hold either an integer value or a null reference. In contrast to a regular int, which cannot be assigned a null value, a nullable int gives you the flexibility to represent the absence of a value in addition to any valid integer.

Syntax

There are different data structures with which we can use nullable int. Below is the syntax of using nullable int with the different data structures.

Array: Declare an array of nullable ints.

int?[] nullableIntArray = new int?[5];

List: Declare a list of nullable ints.

List<int?> nullableIntList = new List<int?>();

Dictionary: Declare a dictionary with nullable int values.

Dictionary<string, int?> nullableIntDictionary = new Dictionary<string, int?>();

HashSet: Declare a HashSet of nullable ints.

HashSet<int?> nullableIntHashSet = new HashSet<int?>();

Let’s understand nullable int with some coding examples. We’ll cover two examples in which we’ll cover how to declare an int nullable in the first example and how to use it with a list.

Coding example: declaring nullable int

Below is a simple example of using int?.

using System;
class Program
{
static void Main()
{
int? nullableInt = null; // assigning null to a nullable int
int? anotherNullableInt = 42; // assigning an integer value
if (nullableInt.HasValue)
{
Console.WriteLine("Value of nullableInt: " + nullableInt.Value);
}
else
{
Console.WriteLine("nullableInt is null");
}
if (anotherNullableInt.HasValue)
{
Console.WriteLine("Value of anotherNullableInt: " + anotherNullableInt.Value);
}
else
{
Console.WriteLine("anotherNullableInt is null");
}
}
}
Declaring nullable int
  • Line 7: nullableInt is assigned null, indicating the absence of a value.

  • Line 8: anotherNullableInt is assigned the integer value 42.

  • Line 19: The HasValue property is used to check whether the nullable int contains a value before attempting to access it.

Coding example: declaring nullable int with list

The below example demonstrates using nullable int with the list data structure.

using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// Creating a List of nullable integers
List<int?> nullableIntList = new List<int?> { 1, 2, 3, null, null, 4, 5 };
// Displaying the values in the list in one line
Console.WriteLine("List: " + string.Join(", ", nullableIntList.Select(x => x.HasValue ? x.ToString() : "null")));
}
}
Declaring nullable int with List
  • Line 10: List<int?> is used to create a list that can store nullable integers. The list is then populated with a mix of nullable integers and null values.

  • Line 13: HasValue property is checked to determine whether each element is a valid integer or null. The values are then displayed accordingly.

Conclusion

Nullable types in C# offer flexibility for managing valid values and the absence of a value, applicable in scenarios like database fields and user input. The syntax involves appending “?” to the underlying type, enhancing code robustness. In scenarios like level order tree traversal, nullable types elegantly represent the absence of child nodes, seamlessly accommodating scenarios where values might be optional or missing.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved