Nullable Types are introduced in C#2.0, they allow you to assign null to a value type variable. In C#, You cannot assign null to a value type variable, if you write something like this i.e int x = null , then this will give compile time error. However, You can declare a value type as Nullable Type and then assign null value.
Before going through in details, it is important to know Reference types and Value Types. If you haven’t gone through my previous article on Reference type and Value type then, please go through here.
Syntax & Usage ?
You can declare nullable type using Nullable<T>
where T is a type, for example.
1 2 3 4 |
Nullable<Int32> i = null; //Declared a null-able type of System.Int32 Nullable<DateTime> dtJoin = null; //Declared a null-able type of System.DateTime Nullable<Boolean> bStatus = null; //Declared a null-able type of System.Boolean Nullable<Decimal> Salary = null; //Declared a null-able type of System.Decimal |
We can also declare nullable type using question mark (?) operator as follows.
1 2 3 4 |
Int32? i = null; //Declared a null-able type of System.Int32 DateTime? dtjoin = null; //Declared a null-able type of System.DateTime Boolean? bStatus = null; //Declared a null-able type of System.Boolean Decimal? Salary = null; //Declared a null-able type of System.Decimal |
Example : Here in below example, we have declared a variable as int i = 0
. Now if you write
int i = null then compiler throws error as shown below.

We can declare above value type as Nullable and assign null value at run time.
In below example code, we have declared a Nullable of type int and assigned null value at run-time.

Nullable types are mostly applicable to value types, you can’t declare a reference type as Nullable , because by default reference types supports null value. In below example System.String is a reference type so compiler throws error.

Conversion of Nullable Type :
C# allows to perform conversions and casts on Nullable types. Here in below, implicit conversion from non-nullable Int32 to Nullable<Int32> .
1 |
Int32? i = 100; //Implicit conversion from non-nullable Int32 to Nullable<Int32> |
Similarly in below example, explicit conversion Nullable<Int32> to non-nullable Int32 .
1 |
Int32 j = (Int32)i //Explicit conversion from Nullable<Int32> to non-nullable Int32 |
HasValue() property of Nullable Type :
The Nullable types are instances of System.Nullable<T>
struct. It has a property as HasValue . It returns True, if the variable contains a value and returns False if it’s null.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Program { static void Main(string[] args) { Nullable i = null; //Nullable declaration int? j = null; //Nullable declaration i = 123;//Assign a value if (i.HasValue)//Check if i has value in it Console.WriteLine("i containes value as - " + i); else Console.WriteLine("i containes null."); if (j.HasValue)//Check if j has value in it Console.WriteLine("j containes value as - " + j); else Console.WriteLine("j containes null."); } } |
Output :

Where to use Nullable Type ?
Lets say you have a boolean field in your database and that also accept null value along with True/False In that case you can define a Nullable of type bool and pass the null value as desired.
1 |
bool? isActive = null; |
Important Points to be noted :
Below are some important points.
- Nullable types can only be used with value types.
- The HasValue property of Nullable type returns True, if the variable contains a value and returns False if it’s null.
- Nullable<T> object returns type T instead of Nullable<T>. In below sample code, we have defined a nullable type and fetching the type information at runtime using GetType() method. When you run below program then, it will print System.Boolean instead of Nullable<Boolean>
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; namespace NullableType { class Program { static void Main(string[] args) { Nullable<Boolean> bStatus = false; Console.WriteLine(bStatus.GetType()); //Displays System.Boolean } } } |
Thanks for your time 🙂 feel free to provide your feedback and do check my other blogs on Dynamic type , Var type , Anonymous type and Reference Type and Value type .