articles

Home / DeveloperSection / Articles / Nullable Variables Null Colecasing Operator

Nullable Variables Null Colecasing Operator

Anurag Chaurasia8751 15-Feb-2011

Nullable Variable: - Nullable Variable is those variables in which the initial value will null and the value will be initialize after quite some time. With the help of nullable variable operator we can declare any type of variable value as null.

Declaration of nullable variable:

int ?a =null;//a is the nullable variable here and ? is the nullable variable operator.

Null Colecasing Operator: - With the help of null coleascing operator we assign a value into null variable if it will have a null value in it then it will assign a different variable value otherwise the default value will be assigned.

Declaration of Null Colecasing Operator :

int b = 45;

int? c = null;

int d = c ?? b;

Sample Code :
using System;
using System.Collections.Generic;
using System.Text;
 
namespace ConsoleApplication1
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            int ?a =null;// This is how we can implement a null value in the integer type variable by using nullable type variable
            int b = 45;
            a = b;
            if (a.HasValue)
            {
                Console.WriteLine("The value of a is : " + a);
            }
            int? c = null;
            int d = c ?? b;
           
            /*
             *  This is the implementation of null coleacsing operator
             *  A null coleascing operator cxhecks that a variable has a null value or not if it will
             *  have a null Value then it will put the default variable value of varible [b]
              */
            Console.WriteLine("The value of d : " + d);
        }
    }
}

 


Updated 17-Jul-2019

Leave Comment

Comments

Liked By