Short note on C# NULLABLE types

Questpond
Posted by in .NET Framework category on for Beginner level | Points: 250 | Views : 6193 red flag
Rating: 3 out of 5  
 1 vote(s)

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. 
 
Page copy protected against web site content infringement by Copyscape

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)