How to Use the Null-Conditional Operator ?. in C#
name.Length throws a NullReferenceException when name is null. Write name?.Length instead and C# checks first: it reads Length when there's a name, and gives back null when there isn't.
Syntax
<result> = <value>?.<Member>;
<result> = <collection>?[<index>];
| Part | What it does |
|---|---|
<value>?.<Member> | Reads <Member> when <value> isn't null; otherwise the whole expression is null. |
<collection>?[<index>] | The same check for an element: reads <collection>[<index>] only when <collection> isn't null. |
<result> | Receives the member's value or null. For a member of a value type, such as int, the result is the nullable int?. |
Microsoft's member access operators reference: "A null-conditional operator applies a member access (?.) or element access (?[]) operation to its operand only if that operand evaluates to non-null. Otherwise, it returns null."
The Example
The example reads the Length of a name twice: once while the name is null and once after it's set to Leo. It then stores the result in a variable and checks whether it holds a value.
Prerequisites
- The .NET 10 SDK or newer, which runs a single
.csfile withdotnet run. This guide uses .NET SDK 10.0.404.
Step 1: Write the Code
Create a file named main.cs in an empty folder:

string? name = null;
Console.WriteLine(name?.Length);
name = "Leo";
Console.WriteLine(name?.Length);
int? length = name?.Length;
Console.WriteLine(length.HasValue);
int? is an int that may also be null. HasValue tells you which one it holds.
Step 2: Run It
dotnet run main.cs
3
True

The first line is empty: name was null, so name?.Length was null too, and Console.WriteLine printed nothing, without an exception. After name = "Leo", the same expression returned 3, and length held a value. The command is the same in zsh, bash and PowerShell.
Why the Result Is an int?
Length is an int, but name?.Length can also be null, so its type is int?. The reference: "If a.x or a[x] is of a non-nullable value type T, a?.x or a?[x] is of the corresponding nullable value type T?." Storing it in a plain int doesn't compile:
string? name = "Leo";
int length = name?.Length;
Console.WriteLine(length);
/Users/you/demo/main.cs(2,14): error CS0266: Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)
/Users/you/demo/main.cs(2,14): warning CS8629: Nullable value type may be null.
The build failed. Fix the build errors and run again.
Use int?, as the example does, or give a fallback for null with ??, as in int length = name?.Length ?? 0;.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Storing name?.Length in an int | error CS0266: Cannot implicitly convert type 'int?' to 'int' | Store it in an int?, or add ?? 0 |
Writing name.Length for a value that may be null | NullReferenceException when it's null | Write name?.Length |
Printing the result without handling null | An empty line, which is easy to miss | Check HasValue, or give a fallback with ?? |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
CS0266 with int? and int | A null-conditional result stored in a non-nullable type | Use int? or ?? <fallback> |
| An empty line where you expected a number | The value before ?. was null | Check where the value should have been set |
FAQ
What happens in a longer chain like a?.B.C?
The check stops the whole chain. The reference: "The null-conditional operators are short-circuiting. That is, if one operation in a chain of conditional member or element access operations returns null, the rest of the chain doesn't execute."
Does ?. work on method calls?
Yes. name?.ToUpper() calls ToUpper only when name isn't null, and is null otherwise.
Conclusion
value?.Member reads the member only when the value isn't null, and returns null instead of throwing. For a value-type member such as Length, the result is an int?: keep it in an int?, or turn null into a default with ??.
Additional Resources
- Null-conditional operators, on Microsoft Learn.
