Null-Conditional Assignment in C# 14 — Elegance Meets Safety
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(); } Or use a ternary: customer?.Order = customer is not null ? GetCurrentOrder() : null; Both approaches were verbose or redundant. The C# 14 Solution Now you can write: customer?.Order = GetCurrentOrder(); 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 Dictionary Case with Null-Conditional Indexer Dictionary? scores = GetScores(); scores?["math"] = 95; // Works only if scores is not null 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

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();
}
Or use a ternary:
customer?.Order = customer is not null ? GetCurrentOrder() : null;
Both approaches were verbose or redundant.
The C# 14 Solution
Now you can write:
customer?.Order = GetCurrentOrder();
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
Dictionary Case with Null-Conditional Indexer
Dictionary<string, int>? scores = GetScores();
scores?["math"] = 95; // Works only if scores is not null
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 |