Nullable Types in C#.NET With C# Nullables Example

Nullable Types in C#.NET With C# Nullables Example

Today, We want to share with you Nullable Types in C#.NET With C# Nullables Example.
In this post we will show you C# Nullables, hear for How to work with nullable types in C# we will give you demo and example for implement.
In this post, we will learn about Nullables In C# with example and description with an example.

Introduction: Nullable type in C#

In this post, we will learn about Nullables In C# with an example.C#.Net support a special data types, the nullable types, to which you can assign normal range of values as well as null values.

For example C# Nullables, you may store any integer value from -2,147,483,648 to 2,147,483,647 or null in a Nullable variable. Same as, you can assign true, false, or null in a Nullable variable.

Syntax:

<data_type> ? = null;

Now open Visual Studio and create Console Application and write below line of code.

Read Also:  Simple Eloquent Laravel One to One Relationship

using System;
using System.Collections;

namespace ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int? _Number1 = null;
            int? _Number2 = 78;
            double? _Number3 = new double?();
            double? _Number4 = 3.14157;

            bool? _boolval = new bool?();

            // display the values
            Console.WriteLine("All value : {0}, {1}, {2}, {3}", _Number1, _Number2, _Number3, _Number4);
            Console.WriteLine("A Nullable boolean value: {0}", _boolval);
            Console.ReadLine();  
        }
    }   
}

The Null Coalescing Operator (??)

The null coalescing operator is used with the nullable value types and reference types. It is used for converting an operand to the type of another C# Nullables nullable (or not) value type operand, where an implicit conversion is possible.

Read Also:  WooCommerce Get list of all product categories

If the value of the first operand is null, then the operator returns the value of the second operand, otherwise it returns the value of the first operand. The following example explains this −

using System;
using System.Collections;

namespace ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            double? _Num1 = null;
            double? _Num2 = 3.14157;
            double _Num3;
            _Num3 = _Num1 ?? 5.34;
            Console.WriteLine(" Value of _Num3: {0}", _Num3);
            _Num3 = _Num2 ?? 5.34;
            Console.WriteLine(" Value of _Num3: {0}", _Num3);
            Console.ReadLine();
        }
    }   
}

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Example of Nullable Types in C#.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.