Author : Rahul
Last Modified : 14-Jan-2021
Complexity : Beginner

Null-Coalescing Operator in C#


In C#, ?? operator is known as Null-coalescing operator.

Syntax: var result = a ?? b;

Description: The given syntax will work as below: 

  1. If the value of a is not NULL, it will return the value of a.
  2. If the value of a is NULL, it will return the value of b.

In other words, this operator returns the first non-null value. 

Important Points: If we want to assign some default value into a variable, this operator is very useful. We can not overload this operator and it is right-associative. We can use it with value types as well as reference types.

Example

Use of ?? operator with value types and reference types

using System;

namespace example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Reference types 
            string str1 = null;
            string str2 = "Rahul";
            string str3 = str1 ?? str2;

            Console.WriteLine("Value of str3 is: {0}", str3);

            // Value types 
            int? val1 = null;
            int? val2 = 10;
            int? val3 = val1 ?? val2;

            Console.WriteLine("Value of val3 is: {0}", val3);
        }
    }
}

Output:

Value of str3 is: Rahul 
Value of val3 is: 10

With the help of ?? operator we can stop InvalidOperationException.

using System;

namespace example
{
    class program
    {
        static void Main(string[] args)
        {
            int? val1 = null;
            //If we run below line, it will give InvalidOperationException
            //int? val2 = val1.Value;

            // With the help of ?? operator we give default value to the val2 when val1 is null
            int? val2 = val1 ?? 10;
            Console.WriteLine("Value of val1 is: {0}", val1);
            Console.WriteLine("Value of val2 is: {0}", val2);
        }
    }
}
Output:

Value of val1 is: 
Value of val2 is: 10

With the help of ?? operator we can remove the “if-else” statement.

using System;

namespace example
{
    class program
    {
        static void Main(string[] args)
        {
            int? val1 = null;

            int? val2;

            // unnecessary if-else statement
            if (val1.HasValue)
            {
                val2 = val1;
            }
            else
            {
                val2 = 10;
            }

            //uses of ?? to remove if-else statement
            int? val3 = val1 ?? 10;

            Console.WriteLine("Value of val1 is: {0}", val1);
            Console.WriteLine("Value of val2 is: {0}", val2);
            Console.WriteLine("Value of val3 is: {0}", val3);
        }
    }
}

Output:

Value of val1 is: 
Value of val2 is: 10

With the help of ?? operator we can remove multiple “if-else” statement.

using System;

namespace example
{
    class program
    {
        static void Main(string[] args)
        {
            int? val1 = null;
            int? val2 = null;
            int? val3 = 10;

            // Nested ?? operator 
            // First it will check the value of val1. if it is null control move to val2 and check val2
            // Now if val2 is null, control will move to val3 and since it is the last operand so the value of val3 will be returned.
            int? val4 = val1 ?? val2 ?? val3;

            val1 = null;
            val2 = 20;
            val3 = 10;

            // Nested ?? operator 
            // First it will check the value of val1. If it is null control move to val2 and check val2
            // Now since val2 is not null, so the value of val2 will be returned.
            int? val5 = val1 ?? val2 ?? val3;

            Console.WriteLine("Value of val5 is: {0} ", val5);
        }
    }
}

Output:

Value of val4 is: 10 
Value of val5 is: 20