Open In App

C# | Nullable types

In C#, the compiler does not allow you to assign a null value to a variable. So, C# 2.0 provides a special feature to assign a null value to a variable that is known as the Nullable type. The Nullable type allows you to assign a null value to a variable. Nullable types introduced in C#2.0 can only work with Value Type, not with Reference Type

The nullable types for Reference Type is introduced later in C# 8.0 in 2019 so that we can explicitly define if a reference type can or can not hold a null value. This helped us to tackle the issue of NullReferenceException without using conditionals. In this article, the discussion revolves around the nullable types for value types.



The Nullable type is an instance of System.Nullable<T> struct. Here T is a type which contains non-nullable value types like integer type, floating-point type, a boolean type, etc. 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;           

How to 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# program to illustrate Nullable Types
using System;
 
class Geeks {
 
    // Main Method
    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

Example:




// C# program to illustrate the
// use of Nullable type
using System;
 
class GFG {
 
    // Main Method
    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

Example:




// C# program to illustrate the
// use of Nullable<L>.Hasvalue
using System;
 
class GFG {
 
    // Main Method
    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

Example:




// C# program to illustrate the
// use of  null-coalescing operator(??)
using System;
 
class GFG {
     
    // Main Method
    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

Advantage of Nullable Types: 

 


Article Tags :
C#