Null-Conditional Assignment

December 15, 2025#Software Development
Article
Author image.

Barret Blake, Architect

Throughout the history of coding we’ve had to deal with occasions when a variable might be null. In C#, not correctly handling null values can often lead to unexpected errors at runtime that weren’t accounted for in the code. There are a lot of ways we can check in our code to see if a variable is null before trying to do something with it.

For one, there’s the really old-school and long winded way:

if(user is not null && user.Address is not null){
    result = user.Address.Street;
}

We could also simplify it with the null-conditional operator to a single line:

string result = user?.Address?.Street;

Either one of these pieces of code work the same way. It checks value of user. If user isn’t null, it then checks the value of Address, inside of the user variable. If neither are null, it will assign the value of Street to result. Either version is “short-circuiting”, meaning if the first condition doesn’t pass, it doesn’t bother to continue to check further conditions. It’s an established pattern, and has been for many years.

But what we couldn’t do was use null-conditional operators on the left side of that operation.

user?.Address?.Street = "123 Any Street";

This would be be a syntax error, and the compiler wouldn’t allow it… until now.

Null-Conditional Assignment

One of the new features of C# 14 is that we now can do null-conditional assignments. Instead of having to write our code like:

if(user is not null && user.Address is not null){
    user.Address.Street = "123 Any Street";
}

Now we can use the null-conditional on the left side of the operation in the assignment portion.

user?.Address?.Street = "123 Any Street";

It also works with array element assignment.

user?.Address?[2].Street = "123 Any Street";

It works the same way as it does on the right side. It checks if user is null. If that’s good, it checks Address. In the case of the array, checking if the Address in element 2 of the array is not null. And if both are good, it assigns the value to Street. If either is null, it skips on and goes on executing with no errors. Its been a long time coming, and a welcome addition to our C# syntactic goodness.

Remember that with arrays, this will still throw an Out of Bounds exception if your array isn’t as big as the position you pass in. Null-conditional assignment doesn’t override that behavior.

You can check out the details in the official Learn documents.

There’s been a lot of focus on C# in recent years to make it more useable and readable, and it’s great to see them continuing to make such welcome additions that make our lives as developers easier.


Copyright © 2025 NimblePros - All Rights Reserved