C# program to demonstrate the example of Nullable data types

Here, we are going to learn about the Nullable data types and its C# implementation.
Submitted by Nidhi, on September 11, 2020

Here we will create nullable variables; variable declared as a nullable type that can store normal values as well as null values, here we will question mark to declare a variable as a nullable.

Program:

The source code to demonstrate the Nullable data types is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//Program to demonstrate the Nullable data types in C#

using System;

class NullableDemo
{
    static void Main(string[] args)
    {
        int? intVal1 = null;
        int? intVal2 = 786;

        float?  floatVal1 =  3.14F;
        float? floatVal2 = new float?();

        bool?   boolval = new bool?();
        
        Console.WriteLine("Nullable Integers : {0}, {1}",intVal1,intVal2);
        Console.WriteLine("Nullable Floats   : {0}, {1}", floatVal1, floatVal2);
        Console.WriteLine("Nullable boolean  : {0}", boolval);
    }
}

Output:

Nullable Integers : , 786
Nullable Floats   : 3.14,
Nullable boolean  :
Press any key to continue . . .

Explanation:

In the above program, we created a class NullableDemo that contains the Main() method. In the Main() method we created nullable variables using a question mark. Variable declared as a nullable type that can store normal values as well as null values, here we will question mark to declare a variable as a nullable. 

Then printed the values of all variables on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.