In this article we will look in to importance of C# NULLABLE types.
Explain need of NULLABLE types ?
It is difficult to assign NULL directly for value types like int , bool , double etc. By using NULLABLE types you can set value types as NULL. To create a NULLABLE type we need to put “?” before the data type as shown in the below code.
int? num1 = null;
In what scenarios we will use NULLABLE types ?
The biggest user of NULLABLE types is when you are reading values from database. Database is one place where there is high possibility of column having NULL’s. So when we want to read those values in to value types NULLABLE types makes it easy as shown in the below code.
while (oreader.Read())
{
int? salary = oreader["Salary"] as int? ;
}
Below is a nice video which demonstrates NULLABLE practically.