0Pricing
C# Academy · Lesson

Null-Conditional Operator

Safe access with ?.

Null-Conditional Operator is a free C# Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Problem of Null Chains

Accessing a member on a null reference throws a NullReferenceException. This is one of the most common runtime crashes in C#.

When you chain calls like user.Address.City, any link in the chain might be null, and the old fix was a tower of if checks.

Meet ?.

The null-conditional operator ?. short-circuits the whole expression. If the left side is null, the result is null instead of an exception.

It reads as: "access this member, but only if the object is not null."

string name = null;
int? length = name?.Length;
System.Console.WriteLine(length == null);

The Result Becomes Nullable

Because ?. can yield null, the result type becomes nullable. name?.Length is int?, not int.

This is why you store it in an int? rather than a plain int.

string text = "hello";
int? len = text?.Length;
System.Console.WriteLine(len);

A Runnable Demo

This program shows ?. returning a value when the object exists, and null when it does not.

Run it and notice that no exception is ever thrown.

using System;

class Program
{
    static void Main()
    {
        string a = "world";
        string b = null;
        Console.WriteLine(a?.ToUpper());
        Console.WriteLine(b?.ToUpper() ?? "(null)");
    }
}

Chaining Multiple ?.

You can chain ?. across several members. As soon as any part is null, the entire chain stops and returns null.

So customer?.Order?.Total is safe even if customer or Order is null.

string s = null;
int? upperLen = s?.ToUpper()?.Length;
System.Console.WriteLine(upperLen == null);

Null-Conditional Indexer ?[]

There is an indexer form, ?[], for arrays and lists. If the collection is null, the index access returns null instead of crashing.

Note: it guards the collection being null, not an out-of-range index.

int[] data = null;
int? first = data?[0];
System.Console.WriteLine(first == null);

Calling Methods Safely

?. also guards method calls. list?.Clear() calls Clear only if list is not null; otherwise it does nothing.

For a method returning void, the whole statement simply becomes a no-op when null.

System.Collections.Generic.List<int> list = null;
list?.Add(5);
System.Console.WriteLine("No crash");

Thread-Safe Event Raising

A classic use is raising events. SomeEvent?.Invoke(this, args) safely fires the event only when at least one handler is subscribed.

This replaced the old null-check pattern that could race between the check and the call.

System.Action handler = null;
handler?.Invoke();
System.Console.WriteLine("Invoked safely");

Combining with ??

?. pairs naturally with the null-coalescing operator ?? to supply a fallback when the chain is null.

name?.Length ?? 0 gives the length, or 0 if name is null.

string name = null;
int length = name?.Length ?? 0;
System.Console.WriteLine(length);

A Subtle Gotcha

Even when the final value type is non-nullable, using ?. makes the expression nullable. list?.Count is int?, not int.

If you need a plain int, follow it with ?? 0 or use GetValueOrDefault().

var list = new System.Collections.Generic.List<int> { 1, 2 };
int count = list?.Count ?? 0;
System.Console.WriteLine(count);

Full Comparison

This program contrasts the verbose if approach with the concise ?. version. Both avoid the exception, but ?. is far cleaner.

using System;

class Program
{
    static void Main()
    {
        string s = null;
        int len = s != null ? s.Length : -1;
        Console.WriteLine(len);
        Console.WriteLine(s?.Length ?? -1);
    }
}

Quick Check

Test your understanding of the null-conditional operator.

Recap

The null-conditional operator ?. (and indexer ?[]) stops a member access when the object is null, returning null instead of throwing.

The result becomes nullable, so combine it with ?? to provide a fallback value.

Frequently asked questions

Is the “Null-Conditional Operator” lesson free?

Yes — the full text of “Null-Conditional Operator” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Null-Conditional Operator”?

Safe access with ?. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Null-Conditional Operator” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Nullable Value Types
  2. Null-Conditional Operator
  3. Null-Coalescing Operators
  4. Guarding Against Nulls
← Back to C# Academy