TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Nullable

C# csharp-nullable for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C-SHARP

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.

  1. By creating System.Nullable instance,
  2. 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

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

Csharp Nullable 1

Run the program by using following command.

NullableExample2.exe

Csharp Nullable 2

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

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





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf