Home »
.Net »
C# Programs
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.
C# Basic Programs »