Two Question Marks in C#: What It Is and How to Use It

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Two Question Marks in C#: Not a Mistake, But a Powerful Tool

\n\n

Imagine: you are writing code in C#, and suddenly you see the ?? construct. Your first thought — a typo? An extra character? In reality, this is one of the most elegant and useful operators in the language — the null-coalescing operator. If you have ever caught a NullReferenceException and cursed everything in sight — this operator will be your lifesaver.

\n\n

In this article, we will break down what two question marks mean in C#, how to use them correctly, how they differ from the ternary operator, and show real-world examples from practice. Let's go!

\n\n

What is the ?? Operator in C#?

\n\n

The ?? operator (two question marks) is a binary operator that returns the left operand if it is not null; otherwise, it returns the right operand. Simply put: "If the left side is not null — take the left side, otherwise take the right side."

\n\n

The syntax is extremely simple:

\n\n
var result = left ?? right;
\n\n

Where:

\n
    \n
  • left — a value that can be null;
  • \n
  • right — a default value (cannot be null if the right side type is nullable).
  • \n
\n\n

First Example: Saving Yourself from null

\n\n

Suppose you have a variable string? name (nullable string). You want to display the user's name, and if it's absent — "Guest":

\n\n
string? name = GetUserName(); // may return null\nstring displayName = name ?? "Guest";\nConsole.WriteLine(displayName);
\n\n

Without the ?? operator, you would have to write:

\n\n
string displayName = (name != null) ? name : "Guest";
\n\n

Admit it, the first option reads like a normal sentence: "name or guest". Elegant, right?

\n\n

The ??= Operator: The Next Generation of Null-Coalescing

\n\n

Starting with C# 8.0, the ??= operator appeared — this is a shorthand assignment form with null-coalescing. It assigns the value of the right operand to the left operand only if the left operand is null.

\n\n

Syntax:

\n\n
left ??= right;
\n\n

This is equivalent to:

\n\n
if (left == null) left = right;
\n\n

Example with ??=

\n\n
List<int>? numbers = null;\nnumbers ??= new List<int>(); // numbers is now = new List<int>()\nnumbers.Add(42);\nConsole.WriteLine(numbers.Count); // 1
\n\n

Note: if numbers were already initialized (not null), the assignment would not occur. This is perfect for lazy initialization.

\n\n

Comparison with the Ternary Operator (? :)

\n\n

Many beginners confuse ?? with the ternary operator ? :. Let's settle this once and for all:

\n\n
    \n
  • Ternary operator — is a shorthand form of if-else. It checks ANY boolean condition: condition ? value_if_true : value_if_false.
  • \n
  • The ?? operator — checks ONLY for null. It is a specific case, tailored for one task.
  • \n
\n\n

Example of the ternary operator:

\n\n
int age = 18;\nstring status = (age >= 18) ? "Adult" : "Minor";
\n\n

And here is what happens if you try to replace ?? with the ternary:

\n\n
string? name = null;\nstring displayName = name != null ? name : "Guest"; // works, but longer\nstring displayName2 = name ?? "Guest"; // shorter and clearer
\n\n

Conclusion: use ?? when you simply need to substitute a default value instead of null. For all other conditions — use the ternary operator.

\n\n

Chaining the ?? Operator

\n\n

You can chain multiple ?? operators together. This is convenient when there are several possible sources for a value:

\n\n
string? firstChoice = GetFromCache                

Blogs

Book Recommendations