DEV Community

Cristian Sifuentes
Cristian Sifuentes

Posted on

Null-Conditional Assignment in C# 14 — Elegance Meets Safety

Null-ConditionalAssignmentInCSharp14

Null-Conditional Assignment in C# 14 — Elegance Meets Safety

C# 14 enhances its elegant null-safety tools by allowing null-conditional operators ?. and ?[] to appear on the left-hand side of assignment expressions.

This new capability removes repetitive null checks and brings a new level of composability and safety to common mutation patterns in object graphs and collections.

In this post, you’ll explore:

  • What is null-conditional assignment
  • What's new in C# 14
  • Real-world usage examples
  • Supported assignment operations
  • Limitations and best practices

The Problem in Pre-C# 14

Before C# 14, you needed explicit null checks before assigning to a property:

if (customer is not null)
{
    customer.Order = GetCurrentOrder();
}
Enter fullscreen mode Exit fullscreen mode

Or use a ternary:

customer?.Order = customer is not null ? GetCurrentOrder() : null;
Enter fullscreen mode Exit fullscreen mode

Both approaches were verbose or redundant.


The C# 14 Solution

Now you can write:

customer?.Order = GetCurrentOrder();
Enter fullscreen mode Exit fullscreen mode

This means:

✔️ "Assign only if customer is not null"

✔️ The right-hand side GetCurrentOrder() is not evaluated if customer is null


Practical Example

Customer-Order Scenario

Customer? customer = GetCustomer();

customer?.Order = CreateOrder(); // If customer is not null
Enter fullscreen mode Exit fullscreen mode

Dictionary Case with Null-Conditional Indexer

Dictionary<string, int>? scores = GetScores();

scores?["math"] = 95; // Works only if scores is not null
Enter fullscreen mode Exit fullscreen mode

Compound Assignments Supported

C# 14 allows null-conditional usage with:

Operation Example Notes
+= customer?.Total += 10; Only if customer isn't null
-= scores?["math"] -= 5; Only if scores isn't null
*= data?.Weight *= 2; Avoids null exception

🔒 NOT supported:

  • ++, -- increment/decrement operators
  • Deconstruction on ?.

Limitations

Limitation Explanation
No support for ++ / -- These operators require direct memory access
Right-hand is not evaluated if null Avoids side effects when reference is null
Only applies to reference or nullable types Value types need to be boxed or wrapped

✅ Best Practices

  • Use ?. in assignment chains to reduce null-guarding boilerplate.
  • Combine with nullable reference types (?) for extra clarity and safety.
  • Avoid side effects in right-hand expressions unless guarded elsewhere.
  • Prefer compound null-conditional updates when mutating optional structures.

Learn More


Final Thoughts

Null-conditional assignment is a small but powerful enhancement — it brings concise null safety to property and collection assignments in a truly modern C# way.

In complex domain models, optional structures, or UI state updates, this feature helps you write less code, with fewer bugs.

Learn it, love it — and let ?. be your new favorite assignment guard.


Written by: [Cristian Sifuentes] – .NET Architect | Fluent API Enthusiast | Null-Safety Advocate
Using this in your ViewModels, services, or pipelines? Let us know how!

Top comments (0)