Nullable Types

C# Nullable

In C#, Nullable is a concept that allows a type to hold an additional value null . In other words, we can make a variable Nullable, so, it can hold additional null value. All Nullable variables are the instances of System.Nullable<T> struct.

The concept of Nullable is useful when we deal with databases that contain elements that may not be assigned a value.

C# provides two different ways to create Nullable types.

  • By creating System.Nullable instance,
  • By using ? operator
  • Note: we cannot create Nullable of reference type variable.

    C# System.Nullable Example

In the following example, we are making Nullable with the help of System.Nullable namespace.

// NullableExample2.cs

Example

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:

Compile the program by using the following command.

csc NullableExample2.cs

Run the program by using following command.

NullableExample2.exe

C# Nullable using ? Operator Example 1

There is no significant difference between using of either System.Nullable or ? operator. We can use anyone as per our comfort.

// NullableExample.cs

Example

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:

Output

10
a contains null value

C# Nullable using ? Operator Example 2

Example

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:

Output

10
It contains null value

Input Required

This code uses input(). Please provide values below: