Open In App

Nullable types in C#

Last Updated : 06 Sep, 2025
Comments
Improve
Suggest changes
18 Likes
Like
Report

In C#, value types (like int, float, bool) normally cannot store null. To solve this, C# 2.0 introduced nullable types using System.Nullable<T>, which let value types hold either their normal range of values or null (e.g., int? x = null;).

For reference types, nullable reference types were added in C# 8.0 to clearly specify whether a reference can be null, helping avoid NullReferenceException. For example, in nullable of integer type you can store values from -2147483648 to 2147483647 or null value. 

Syntax

Nullable<data_type> variable_name = null;

Or you can also use a shortcut which includes ? operator with the data type.

datatype? variable_name = null;

Example:  

// this will give compile time error
int j = null;

// Valid declaration
Nullable<int> j = null;

// Valid declaration
int? j = null;

Access the value of Nullable type variables

You cannot directly access the value of the Nullable type. You have to use GetValueOrDefault() method to get an original assigned value if it is not null. You will get the default value if it is null. The default value for null will be zero.

Example: 

C#
using System; 

class Geeks { 

    static void Main(string[] args)
    {
        
        // defining Nullable type
        Nullable<int> n = null;

        // using the method output will be 0 as default value of null is 0
        Console.WriteLine(n.GetValueOrDefault()); 
        
        // defining Nullable type
        int? n1 = null;

        // using the method output will be 0 as default value of null is 0
        Console.WriteLine(n1.GetValueOrDefault()); 
        
         
        // using Nullable type syntax to define non-nullable
        int? n2 = 47;

        // using the method
        Console.WriteLine(n2.GetValueOrDefault()); 
        
        // using Nullable type syntax to define non-nullable
        Nullable<int> n3 = 457;

        // using the method
        Console.WriteLine(n3.GetValueOrDefault()); 
        
    }
} 

Output
0
0
47
457

Characteristics of Nullable types

  • With the help of nullable type you can assign a null value to a variable without creating nullable type based on the reference type.
  • In Nullable types, you can also assign values to nullable type. As shown in the below example.

Example 1:

C#
using System;

class GFG {

    static public void Main()
    {
        // a is nullable type and contains null value
        int ? a = null;

        // b is nullable type int and behave as a normal int
        int ? b = 2345;

        // this will not print anything on console
        Console.WriteLine(a);
        
        // gives 2345 as output
        Console.WriteLine(b);
    }
}

Output
2345
  • You can use Nullable.HasValue and Nullable.Value to check the value.
  • If the object assigned with a value, then it will return "True" and if the object is assigned to null, then it will return "False".
  • If the object is not assigned with any value then it will give compile-time error.

Example 2:

C#
using System;

class GFG {

    static void Main()
    { 
        // a is nullable type and contains null value
        Nullable<int> a = null;

        // check the value of object
        Console.WriteLine(a.HasValue);
        
        // b is nullable type and contains a value
        Nullable<int> b = 7;

        // check the value of object
        Console.WriteLine(b.HasValue);
    }
}

Output
False
True
  • You can also use == and ! operators with nullable type.
  • You can also use GetValueOrDefault(T) method to get the assigned value or the provided default value, if the value of nullable type is null.
  • You can also use null-coalescing operator(??) to assign a value to the underlying type originate from the value of the nullable type.

Example 3:

C#
using System;

class GFG {
    
    static public void Main()
    {
        // a is nullable type and contains null value
        int ? a = null;
        
        // it means if a is null then assign 3 to b
        int b = a ?? 3;
        
        // It will print 3
        Console.WriteLine(b);
    }
}

Output
3
  • Nullable types do not support nested Nullable types.
  • Nullable types do not support var type. If you use Nullable with var, then the compiler will give you a compile-time error.

Advantage of Nullable Types

  • The main use of nullable type is in database applications. Suppose, in a table a column required null values, then you can use nullable type to enter null values.
  • Nullable type is also useful to represent undefined value.
  • You can also use Nullable type instead of a reference type to store a null value.
Suggested Quiz
4 Questions

Which of the following declarations is valid for creating a nullable integer in C#?

  • A

    int j = null;

  • B

    Nullable<int> j = null;

  • C

    int? j = null;

  • D

    Both B and C

Explanation:


What does the method GetValueOrDefault() return when the nullable variable contains null?

  • A

    Throws an exception

  • B

    Returns null

  • C

    Returns the default value of the underlying type

  • D

    Returns -1

Explanation:


What will be the output of the following code?

C#
int? a = null;
int result = a ?? 10;
Console.WriteLine(result);


  • A

    null

  • B

    0

  • C

    10

  • D

    Compile-time error

Explanation:


Which property is used to check whether a nullable variable contains a value?

  • A

    .IsNull

  • B

    .HasValue

  • C

    .Exists

  • D

    .CheckValue

Explanation:


Image
Quiz Completed Successfully
Your Score :   2/4
Accuracy :  0%
Login to View Explanation
1/4 1/4 < Previous Next >

Article Tags :

Explore