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

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.

🎓 Free Certifications on 300 subjects are live for December 2025. Register Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification 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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.