Today I’m here to talk about a feature that will be available in C# 14 along with the arrival of .NET 10 in November 2025. But as of now, we can already try it out if we use the preview version of the language. The null conditional assigner for C#.
Since this is a preview version (until November 25th), do NOT use it in production. If you want me to keep making videos showing new features, leave a comment below, and well, if you’d prefer a summary video in November with all the new stuff, let me know that too, although I’ll definitely make that summary every year regardless.
1 - Working with nulls in C#
Within the C# language we have several ways to deal with null values; in fact, I have a post from 2019 where I already talked about them. Since then, several key features have come out in the language itself.
We’ve changed several times how we check if a value is null; in fact, I have a post about that, which I’ll need to update to link this very article.
But we haven’t just changed how we check for null values; we’ve also changed how we define them in the code. With the arrival of the nullable
property, we can define when a type can be null or not. For example, the following code:
Int? age = person?.Age;
This will check if the variable person is null or not, and if it isn’t, it assigns the value of .Age
to the variable, and since it may be null, we specify it with ?;
This feature isn’t mandatory; in fact, it comes enabled by default, but in my opinion it’s crucial for writing good code in C#.
2 - Assigning null values in .NET
We saw in the previous section that we can assign a null value to a variable, and the compiler does an if behind the scenes to check whether the value is null or not.
But what happens if, instead of a variable, we do this to the property of an object?
For example, we have the following code:
Person person = new person();
person.Age = 18;
We’re assigning the value 18
to the Age
property of the person
object.
We can see this object is not null because we’re instantiating it right there, but what happens if, instead of being instantiated, we’re getting it in a method? For example, maybe we read it from a database or receive it from an external service.
Note: This is a simplified scenario, in reality, we shouldn’t operate on it, but for now this is just an example.
If we write the following code:
public void AssignFixedAge(Person person)
{
person.Age = 18;
}
If the person variable is null, the code will break at runtime. So what we’ll do is mark the variable as nullable and use an if in the code:
public void AssignFixedAge(Person? person)
{
if(person is not null)
{
person.Age = 18;
}
}
This is where the new feature comes in, it saves us from needing to write the if when assigning a value to a variable that may or may not be null. Now we can use the ? character to avoid writing the entire if, so the previous example becomes:
public void AssignFixedAge(Person? person)
{
person?.Age = 18;
}
And the compiler will do the work for us.
If there is any problem you can add a comment bellow or contact me in the website's contact form