What are nullable data types in C#?

CsharpProgrammingServer Side Programming

C# provides the nullable types, to which you can assign normal range of values as well as null values.

For example, you can store any value from -2,147,483,648 to 2,147,483,647 or null in a Nullable variable. Similarly, you can assign true, false, or null in a Nullable variable.

The following is the syntax −

< data_type> ? <variable_name> = null;

Here is an example −

int? num1 = null;

Let us see the complete example to work with Nullable data types −


 Live Demo

using System;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         int? num1 = null;
         int? num2 = 45;

         double? num3 = new double?();
         double? num4 = 3.14157;

         bool? boolval = new bool?();

         // display the values
         Console.WriteLine("Nullables at Show: {0}, {1}, {2}, {3}", num1, num2, num3, num4);
         Console.WriteLine("A Nullable boolean value: {0}", boolval);


Nullables at Show: , 45, , 3.14157
A Nullable boolean value:
Published on 10-Aug-2018 14:45:41