C# 14: Null-conditional assignment

2/24/2025
1 minute read

The preview 1 of dotnet 10 and therefore the next iteration of the C# language is right in front of our doorsteps. So let's have a look at one of the first potential additions to the language: Null-conditional assignment.

The basic problem it tries to solve is something like this: Imagine you have a variable which might be null. If it isn't you want to assign a property. The way to do it now is:

MyObject? myObj = CallThatMightCreateMyObject();

if (myObj is not null)
{
  myObj.Property = 100;
}

The proposal tries to tackle exactly this - the code above can be shortened to:

MyObject? myObj = CallThatMightCreateMyObject();
myObj?.Property = 100;

So if myObj is not null the assignment will be called, otherwise not. Basically, a fancy compiler sugar for the same code as above.

To be absolutely clear, you can also do those "beautiful" assignments:

MyDeeplyNestedObj? m = FromSomewhere();
m?.A?.B?.C?.D = "Foo Bar";

The first possible new feature of C# 13: Params collection

Only one month after the big release of .NET 8, the dotnet team is already working on the next iteration: .NET 9. With that also comes new language features. The first one is about to be merged into the main development branch: Params Collections.

C# vNext - What could come

C# 11 is in front of our doorsteps. A lot of the features are know, so let us have a look at the future. We will discover some features which could be included in C# 12 or later.

Base for this is the csharplang-repository. So let's discover brand new features and how they could help us in our everyday life. We will see things like:

  • Compound assignments in object initializer
  • Discriminated unions
  • semi-auto properties aka field
  • ConfigureAwait(false) on assembly level
  • Exponentiation operator

Infographics Compendium II - ThrowHelper, null Task and more

This edition has the following infographics:

  • ConfigureAwait on IAsyncDisposable
  • Index in foreach
  • Non-destructive mutations with struct
  • Returning null for a Task
  • Throw-Helper
  • verbatim strings

An error has occurred. This application may no longer respond until reloaded. Reload x