C# 14, coming with .NET 10, introduces null-conditional assignment, a feature that lets you safely assign values to properties or indexers without endless if statements. Learn more about this new feature in this article.
What's New in C# 14 Series What's New in C# 14: User-Defined Compound Assignments
What's New in C# 14: Null-Conditional Assignments
If you've ever developed in C#, you've likely encountered a snippet like the one below:
if (config?.Settings is not null) { config.Settings.RetryPolicy = new ExponentialBackoffRetryPolicy(); }
This check is necessary because, if config or config.Settings is null , a NullReferenceException is thrown when trying to set the RetryPolicy property.
But no more endless if s! The latest version of C#, scheduled for release later this year with .NET 10, introduces the null-conditional assignment operators, which are designed to solve this exact issue.
Wait, Doesn't C# Already Have Null-Conditionals?
Yes! The null-conditional and null-coalescing operators have been around for awhile. They simplify checking if a value is null before assigning or reading it.
// Null-conditional (?.) if (customer?.Profile is not null) { // Null-coalescing (??) customer.Profile.Avatar = request.Avatar ?? "./default-avatar.jpg"; }
... continue reading