Open In App

Null-Conditional Operators in C# (?. and ?[])

Last Updated : 01 Nov, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Null-conditional operators in C# provide a concise and safe way to access members or elements of objects that might be null. They prevent NullReferenceException by short-circuiting the operation when the target object is null.

C# provides two null-conditional operators:

  • ?. : Safely accesses a member (property, field, method or event) of an object that could be null.
  • ?[] : Safely accesses an element of an array or indexer when the object might be null.

If the object is null, the expression evaluates to null instead of throwing an exception.

1. Using the ?. Operator

C#
using System;

public class Student
{
    public string Name { get; set; }
    public int? Age { get; set; }
}

class Program
{
    static void Main()
    {
        Student s = null;

        // Safe member access
        string name = s?.Name;
        int? age = s?.Age;

        Console.WriteLine(name);
        Console.WriteLine(age); 
    }
}

Output:

(null)

(null)

Explanation:

  • s?.Name checks whether s is not null before accessing the Name property.
  • If s is null, the expression simply returns null instead of throwing an exception.
  • You can combine it with the null-coalescing operator to provide a fallback value:
C#
int age = s?.Age ?? 18; // Returns 18 if s or s.Age is null
  • The ?. operator can also be chained through multiple levels of object references:
C#
string city = s?.Address?.City;
  • It can also be used with events for safe invocation without explicit null checks:
C#
EventHandler handler = null;
handler?.Invoke(this, EventArgs.Empty); // Safe event call

2. Using the ?[] Operator

C#
using System;

class Program
{
    static void Main()
    {
        int[] numbers = null;

        // Safe array access
        int? firstNumber = numbers?[0];

        Console.WriteLine(firstNumber);
    }
}

Ouptut:

(null)

Explanation:

  • numbers?[0] tries to access the first element only if numbers is not null.
  • If numbers is null, it returns null and avoids throwing a NullReferenceException.
  • This operator is useful when working with arrays, lists or dictionaries that might not be initialized:
C#
List<string> names = null;
string firstName = names?[0]; // Returns null safely

3. Combining Null-Conditional with Methods

C#
using System;

class Program
{
    static void Main()
    {
        string text = null;

        // Safe method invocation
        int? length = text?.Length;

        Console.WriteLine(length);
    }
}

Output:

(null)

Explanation:

  • text?.Length safely checks whether text is not null before retrieving its length.
  • This prevents runtime exceptions when working with potentially null objects or strings.
  • Similar logic applies to method calls:
C#
int? count = text?.Trim()?.Length;
  • Each operation executes only if the previous result is not null.

Use Cases

  • Accessing nested properties in complex models where intermediate objects can be null.
  • Safely invoking methods or events on possibly uninitialized objects.
  • Working with optional elements in arrays, lists or dictionaries.
  • Simplifying null checks and improving code readability.
Suggested Quiz
3 Questions

What is the main purpose of the null-conditional operator ?. in C#?

  • A

    To throw an exception when the object is null

  • B

    To safely access a member of an object that might be null

  • C

    To convert value types to reference types

  • D

    To automatically initialize null objects

Explanation:


What does the ?[] operator do?

  • A

    Initializes an array with default values

  • B

    Safely accesses an element of an array or indexer if the array might be null

  • C

    Converts arrays to nullable types

  • D

    Throws an exception if the array is null

Explanation:

The ?[] operator allows safe element access. If the array or collection is null, it returns null rather than throwing an exception, avoiding runtime errors.

Which of the following statements is TRUE when using null-conditional operators?

  • A

    You cannot chain multiple ?. operators

  • B

    ?. can be combined with the null-coalescing operator ?? for fallback values

  • C

    ?. automatically initializes the object if it is null

  • D

    ?[] can only be used with arrays, not lists or dictionaries

Explanation:

Null-conditional operators can be combined with ?? to provide a default value if the result is null.

Image
Quiz Completed Successfully
Your Score :   2/3
Accuracy :  0%
Login to View Explanation
1/3 1/3 < Previous Next >

Article Tags :

Explore