C# 14: The Null-conditional Assignment

In the previous blog posts about C# 14 you learned about

In this blog post, you will learn about another C# 14 feature: The null-conditional assignment.

The Null-Conditional Operator

With C# 6, which was released together with .NET Framework 4.6 in the year 2015, Microsoft introduced the Null-Conditional Operator, also known back then as Null Propagation Operator. Let’s take a look at it by using the Person class below as an example.

public class Person
{
    public string? FirstName{ get; set; }
}

Now let’s look at the code below. The condition of the if statement checks if the person’s firstname is Thomas, and if that’s the case, it writes a statement to the console (TCH=Thomas Claudius Huber :-) ).

Person? person = null;

if (person.FirstName == "Thomas")
{
    Console.WriteLine("Hello TCH!");
}

Can you spot the problem with the code above?

Thanks to nullable reference types, you will already get a warning during development in your code editor. It will tell you that the person variable may be null. When you run this piece of code, you get a NullReferenceException when you try to access the Person object’s FirstName property, because the person variable is null.

Before C# 6, you solved this problem with an additional condition like below that checks if the person is not null. Only if that first condition in the if statement evaluates to true, the FirstName property is used and checked:

if (person != null && person.FirstName == "Thomas")
{
    Console.WriteLine("Hello TCH!");
}

With C# 6’s null-conditional operator, you can achieve the same thing by adding a little question mark behind the person variable like you see it below. If the person variable is null, the full person?.FirstName expression evaluates to null. This means you don’t get a NullReferenceException anymore.

if (person?.FirstName == "Thomas")
{
    Console.WriteLine("Hello TCH!");
}

Of course, you can chain multiple question marks in a single expression. Look at the statement below. The expression person?.FirstName?.Length returns the length of the firstname if the person variable is not null and if the FirstName property is not null. Else, the expression evaluates to null. Is that the case, the Null Coalescing Operator (??) is used to fall back to the value zero. This means that the firstNameLength variable contains the length of the FirstName property if the person variable and that property is not null, else it contains the value 0.

var firstNameLength = person?.FirstName?.Length ?? 0;

So far so good, but that stuff was already introduced with C# 6. What about C# 14?

The Null-Conditional Assignment

C# 14 introduces the null-conditional assignment. It’s similar to the null-conditional operator. The difference is that the null-conditional assignment happens on the left-side of the equals sign. Let’s take a look at an example.

In your code, you might have if statements like the one below. If the person variable is not null, its FirstName property is set to the value Thomas.

if (person is not null)
{
    person.FirstName = "Thomas";
}

With C# 14, you can use for exactly this code from above a null-conditional assigment. The code below shows it in action. Note the little question mark after the person variable. Only if the person variable is not null, its FirstName property will be set to Thomas. If the person variable is null, nothing will happen there, you won’t get a NullReferenceException. It’s the same logic as the if statement above.

person?.FirstName = "Thomas";

That’s the feature. It’s simple, but also very special, as it means that you have statements in your code that might not execute. If the question marks on the left side of the equals sign evaluate to null, the statement is not executed. Before C# 14, you covered logic like this with an if statement, now you can write it as a simple (null-conditional) assignment.

Of course, you can also use the null-conditional assigment for nested properties. Let’s say you have the class hierarchy below where a Person has an optional Address object.

public class Person
{
    public Address? Address { get; set; }
}

public class Address
{
    public string? Street { get; set; }
}

Now, what if you want to set the Street property of a person’s address? To do this, you check if the person and also its address is not null. The old-school way looks like below.

if (person is not null && person.Address is not null)
{
    person.Address.Street = "Elmstreet";
}

With C# 14’s null-conditional assigment, you can write the same code like you see it below. The statement below means that you set the Street property only if the person variable and its Address property is not null.

person?.Address?.Street = "Elmstreet";

Conclusion

The null-conditional assigment is another little improvement of the C# language that makes our code more compact and more readable. I’m sure it will replace many of these null-checking if statements in our future C# code.

Happy coding,
Thomas

Share this post

Comments (2)

  • Daniel Reply

    Probably worth noting that heavy usage of the null-conditional operator results in many generated if-statements which “reduces” performance – technically. For example if you want to assign multiple properties of an object that maybe null. Maybe the compiler detects this and group the operators together, but maybe not. Just saying.

    November 7, 2025 at 1:10 pm
    • Thomas Claudius Huber Reply

      Hi Daniel, great thoughts. Yes, absolutely, if you do the same check in multiple statements makes a difference than doing it just once for the whole object. If you do the operation thousands of times and milliseconds matter, it can be important. Could be interesting to look at the generated IL code and see if the C# compiler team optimizes this automatically.

      Thanks for the great input,
      Thomas

      November 12, 2025 at 3:56 pm

Leave a Reply to Daniel Cancel reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.