C# Nullable
In C#, the concept of Nullable enables a type to store a null value in addition to its regular values. This means that a variable can be designated as Nullable, allowing it to accommodate a null value. Every Nullable variable in C# is an object of the System.Nullable<T> structure.
The idea of Nullable proves beneficial when working with databases that house elements that might not have a value assigned to them.
C# offers two distinct methods for generating Nullable types:
- Through the creation of a System.Nullable instance,
- By employing the ? operator.
Note: we cannot create Nullable of reference type variable.
C# System.Nullable Example
In this instance, we are creating a Nullable type using the System.Nullable namespace.
// NullableExample2.cs
using System;
namespace CSharpFeatures
{
class NullableExample2
{
static void Main(string[] args)
{
Nullable<int> a = 10;
Nullable<double> d = 10.10;
Nullable<char> c = 'S';
Nullable<bool> b = false;
// Displaying value
Console.WriteLine(a.Value);
// assigning null values
a = null;
d = null;
c = null;
b = null;
// Checking, does "a" contain value ?
if (a.HasValue)
{
Console.WriteLine(a.Value);
}
if(a == null)
Console.WriteLine("It contains null value");
}
}
}
Output:
Execute the program by utilizing the provided command.
csc NullableExample2.cs
The styling for a placeholder element includes a background with a linear gradient, border radius, padding, margin, and center alignment. Within this element, there is an icon with a size of 3rem and text with a font size of 1rem.
Run the program by using following command.
NullableExample2.exe
The styling for the placeholder diagram includes a background with a linear gradient, rounded corners, padding, margin, and center alignment. Inside the diagram, there is an icon with a specific size and spacing, along with text in a designated color and font size.
C# Nullable using ? Operator Example 1
There is no notable variance in utilizing either System.Nullable or the ? operator. We are free to choose either based on our preference.
// NullableExample.cs
using System;
namespace CSharpFeatures
{
class NullableExample
{
static void Main(string[] args)
{
// Integer nullable
int? a = 10;
// Float nullable
double? f = 10.10;
// Boolean nullable
bool? b = false;
// Char nullable
char? c = 'S';
// Checking value is present or not
if (a.HasValue)
{
Console.WriteLine(a.Value);
}
else Console.WriteLine("a contains null value");
// Assigning null value
a = null;
if (a.HasValue) // Checking again
{
Console.WriteLine(a.Value);
}
else Console.WriteLine("a contains null value");
}
}
}
Output:
10
a contains null value
C# Nullable using ? Operator Example 2
using System;
namespace CSharpFeatures
{
class NullableExample2
{
static void Main(string[] args)
{
Nullable<int> a = 10;
Nullable<double> d = 10.10;
Nullable<char> c = 'S';
Nullable<bool> b = false;
// Displaying value
Console.WriteLine(a.Value);
// assigning null values
a = null;
d = null;
c = null;
b = null;
// Checking, does "a" contain value ?
if (a.HasValue)
{
Console.WriteLine(a.Value);
}
if(a == null)
Console.WriteLine("It contains null value");
}
}
}
Output:
10
It contains null value