Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#

How to use the null-coalescing assignment operator in C#

Ayesha Naeem

The null-coalescing assignment operator ??= in C# 8.0 evaluates its right-hand operand and assigns its value to the left-hand operand if the left-hand operand is null. If the left-hand operand is not null, the right-hand operand remains unevaluated.

Syntax

The value of the right-hand operand b is assigned to the left-hand operand a if a evaluates to null.

Key points about the null-coalescing assignment operator

  1. The left-hand operand of the ??= operator must be one of the following:
  • a variable
  • a property
  • an indexer element
  1. The left-hand operand of the ??= operator must not be a non-nullable type.

  2. It is not possible to overload the null-coalescing assignment operator.

  3. The null-coalescing operator is right-associative. This means that an expression such as a ??= b ??= c will be evaluated as a ??= (b ??= c).

Examples

The following examples demonstrate how to use the ??= operator:

using System;
namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
			// Example 1
			// create a null variable
            string var1 = null;
            string var2 = "Eductaive.io";
			// assign var1 to var3 using ??=
            string var3 = var1 ??= var2;
			//display var3
			Console.WriteLine("Value of var3 is: ");
            Console.WriteLine(var3);
			
			// Example 2
			// create nullable value type
			int ? var4 = null;
			int var5 = var4 ??= 10;
			Console.WriteLine("Value of var5 is: ");
			Console.WriteLine(var5);
			
			// Example 3
			// condense if else statement
			int ? var6 = null;
			int a = 5;
			
			if ( var6 is null)
			{
				var6 = a;
			}
			
			Console.WriteLine("Value of var6 is: ");
			Console.WriteLine(var6);
			
			int ? var7 = null;
			var7 ??= 10;
			
			Console.WriteLine("Value of var7 is: ");
			Console.WriteLine(var7);
			
			
			
			
        }
    }
}

Output

Value of var3 is: 
Eductaive.io
Value of var5 is: 
10
Value of var6 is: 
5
Value of var7 is: 
10

Example 1

The program creates a null string var1 and a string var2 containing “Educative.io”. var1 is assigned to the string var3, and if var1 is null, var2 is indirectly assigned to var3. Since the null-coalescing operator is right-associative, var2 is first assigned to var1, then to var3.

Example 2

The program uses a nullable value type to create the variable var4 and a value type to create the variable var5. Since value type variables cannot take on a null value, the expression in line 21 first sets var4 to 10, then assigns it to var5.

Example 3

Example 3 demonstrates how to condense conditional blocks using the ??= operator. The program first implements the code using a conditional statement to check if var6 is null, and assigns it a number if it is null. The program then executes the same functionality in a single line using the ??= operator.

RELATED TAGS

c#

CONTRIBUTOR

Ayesha Naeem
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring