(C# Language) Null conditional (?. and ?[]) operators

An object reference might be null when your code calls a function or accesses a property. Programmers use an if-condition as a safety net and make a null-test on an object that has a possibility of being null at that point. The null conditional operators make it easier to write the code without the if-condition.
(Rev. 31-Oct-2024)

Categories | About |     |  

Parveen,

Table of Contents (top down ↓)

The basic syntax

Let's see the basic syntax. Consider the operation obj?.fx() where obj is some instance and fx is some function on obj. The null reference operator is now a safety guard. The function fx executes if obj is not null. However, if obj is null then the execution stops and the whole expression evaluates to null because obj is null.

Video Explanation (see it happen!)

Please watch the following youtube video:

Example 1

Consider this program where we have a member function called fx that takes an int parameter and returns the thrice of that number.

Inside Main we have a null instance of the Program class. It has been deliberately set so that we can explain a use of the null conditional operator.

In the next statement we have called the function fx by using a null conditional operator. The null conditional operator now works as a safety guard because p is null, and therefore, execution doesnot proceed to fx. The whole expression returns null, so the variable m gets a null.


using System;

namespace MyConsoleApp
{
  internal class Program
  {
    int fx(int x) => 3 * x;
      
    static void Main(string[] args)
    {
      Program p = null;

      // fx never executes here . . . 
      
      var m = p?.fx(4);

      // the program doesn't fail because (?.) 
      // acts as a safety guard 

      // m is null, nothing prints 
      Console.WriteLine(m);

    }
  }
}

The last line prints nothing because m is null.

We can run this program to verify that the program doesn't crash even though p is null.

Example 2

Next we have an example where an array is accessed.


internal class Program
{

  static void Main(string[] args)
  {
    List<String> listA = new List<string> { "apple", "orange" };

    List<String> listB = null;

    // the ?? null-coalescing operator 
    // can be used to provide a string 
    // in place of a null 
    String strA = listA?[0] ?? "NULL";
  
    String strB = listB?[0] ?? "NULL";
  
    Console.WriteLine($"listA[0] = {strA}");

    Console.WriteLine($"listB[0] = {strB}");

  }
}

This example initializes a listA of two elements.

The second identifier listB is kept null.

Null-conditional operator is used to access the first element of both the lists. The first element of listA should be accessible without problems.

Notice that we have used the null-coalescing operator (??) to provide a readable string value in case a null is returned.

But listB is a null reference. So there should be a problem when an attempt is made to access its first element - there should be program crash. The use of null-reference operator prevents this crash and returns a null value instead.

We can run the program to verify that the program completes successfully. Thanks!


This Blog Post/Article "(C# Language) Null conditional (?. and ?[]) operators" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.