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.

advertisement
advertisement

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 :

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.