Nullable Data Types in C#
Posted by Superadmin on August 14 2022 03:53:48

Nullable Data Types in C#

 

 

This is a C# Program to illustrate nullable data types.

Problem Description

This C# Program Ilustrates Nullable Data Types.

Problem Solution

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

Program/Source Code

Here is source code of the C# Program to Ilustrate Nullable Data Types. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Ilustrate Nullable Data Types
 */
using System;
namespace Application
{
    class Nullables
    {
        static void Main(string[] args)
        {
            int? num1 = null;
            int? num2 = 100;
            double? num3 = new double?();
            double? num4 = 3.14157;
            bool? boolval = new bool?();
            Console.WriteLine("Nullables : {0}, {1}, {2}, {3}",
                               num1, num2, num3, num4);
            Console.WriteLine("A Nullable boolean value: {0}", boolval);
            Console.ReadLine();
 
        }
    }
}
Program Explanation

This C# program is used to illustrate nullable data types. Here C# provides a special data types, the nullable types, to assign normal range of values as well as null values. The null coalescing operator is used with the nullable value types and reference types.

 

It is used for converting an operand to the type of another nullable (or not) value type operand, where an implicit conversion is possible. If the value of the first operand is null, then the operator returns the value of the second operand, otherwise it returns the value of the first operand. Print the nullable data types.

Runtime Test Cases
 
Nullables : ,100,,3.1457
A Nullable Boolean Value :