Nullable type in C#

In this article we will learn about the,

  1. Nullable Types in C#
  2. Null coalescing Operator??

Nullable Type

C# provides us a data type, called Nullable type, so according to this if we assign any variable with nullable type then we can assign normal range value as well as Null value to variable.

In C# there are 2 type of categories,

  1. Value type – int double,float,enum , struct etc.
  2. Reference type – interface, class , delegated,array, etc

Deceleration of a nullable type is,

  1. < data_type> ? <variable_name> = null;  
By default value type variable are non nullable , for making them nullable we use “?” nullable type. For ex 

int i = 0 ( here i is non nullable, so we can not assign null to i, i = null will give a compiler error)

At second place we write that
int? i = 0 (i is nullable int, so i=null is legal and it will not give ant compiler error)

Nullable types is just like a bridge b/w the C# type and database types

Following example show the nullable type in C#,

  1. using System;  
  2.   
  3. using System.Collections.Generic;  
  4.   
  5. using System.Linq;  
  6.   
  7. using System.Web;  
  8.   
  9. namespace NullableType  
  10.   
  11. {  
  12.   
  13. class NullableTypeInCSharp  
  14.   
  15. {  
  16.   
  17. static void Main(string[] args)  
  18.   
  19. {  
  20.   
  21. int? Number1 = null;  
  22.   
  23. int? Number2 = 45;  
  24.   
  25. double? Number3 = new double?();  
  26.   
  27. double? Number4 = 31;  
  28.   
  29. bool? lbnValue = new bool?();  
  30.   
  31. // display the values  
  32.   
  33. Console.WriteLine("Nullables perameters: {0}, {1}, {2}, {3}", Number1, Number2, Number3, Number4);  
  34.   
  35. Console.WriteLine("A C# Nullable boolean value: {0}", lbnValue);  
  36.   
  37. Console.ReadLine();  
  38.   
  39. }  
  40.   
  41. }  
  42.   
  43. }  
The Null Coalescing Operator (??) 

Null coalescing operator(??) is used with the nullable type variable and reference type variable.

Basically it is used for converting an operand to another nullable value type operand ,here an implicit conversion is possible.

Use of this coalescing operator (??) is that If the assign value of the operand is null then operator return the value of 2nd operand, otherwise it return 1st operand value.

Following example is shown the coalescing operator,

  1. using System;  
  2.   
  3. using System.Collections.Generic;  
  4.   
  5. using System.Linq;  
  6.   
  7. using System.Web;  
  8.   
  9. namespace CoalescingTypeInCSharp  
  10.   
  11. {  
  12.   
  13. class CoalescingType  
  14.   
  15. {  
  16.   
  17. static void Main(string[] args)  
  18.   
  19. {  
  20.   
  21. double? number1 = null;  
  22.   
  23. double? number2 = 4.12;  
  24.   
  25. double Number3;  
  26.   
  27. Number3 = number1 ?? 6.12;  
  28.   
  29. Console.WriteLine(" Value of Number3: {0}", Number3);  
  30.   
  31. Number3 = number2 ?? 5.34;  
  32.   
  33. Console.WriteLine(" Value of Number3: {0}", Number3);  
  34.   
  35. Console.ReadLine();  
  36.   
  37. }  
  38.   
  39. }  
  40.   
  41. }  

Next Recommended Reading Nullable Types in C#