C# 14 introduces null-conditional assignment (?.=), a small but highly practical language enhancement that reduces repetitive null checks when updating objects. It builds on the existing null-conditional operator (?.) and allows assignments to occur only when the target object is not null.

This feature improves code readability, reduces boilerplate, and helps prevent NullReferenceException in real-world applications where optional objects are common.

What Is Null-Conditional Assignment (?.=)?

Syntax

C#
target?.Property = value;

Before C# 14: How Null Checks Were Handled

Manual Null Check

C#
if (user != null)
{
    user.IsActive = true;
}

Null-Conditional Operator (Read-Only)

C#
user?.Activate();

C# 14 Null-Conditional Assignment in Action

C#
user?.IsActive = true;

Real-Life Scenarios

1.Optional Logging Service

C#
logger?.LastMessage = "Order processed successfully";

2. UI or ViewModel Updates

C#
viewModel?.StatusMessage = "Loading completed";

3. Cache Metadata Updates

C#
cacheEntry?.LastAccessed = DateTime.UtcNow;

Complex Scenarios

1. Order Processing with Optional Audit Trail

C#
public void CompleteOrder(Order order, AuditTrail audit)
{
    order.Status = OrderStatus.Completed;
    order.CompletedAt = DateTime.UtcNow;

    audit?.LastAction = "Order completed";
    audit?.UpdatedAt = DateTime.UtcNow;
}

2. Middleware with Optional Telemetry

C#
public async Task InvokeAsync(HttpContext context, Telemetry telemetry)
{
    var start = DateTime.UtcNow;

    await _next(context);

    telemetry?.RequestPath = context.Request.Path;
    telemetry?.DurationMs =
        (DateTime.UtcNow - start).TotalMilliseconds;
}

3. Async Service with Optional Cache Sync

C#
public async Task<UserProfile> GetProfileAsync(
    int userId,
    IProfileCache cache)
{
    var profile = await LoadFromDatabaseAsync(userId);

    cache?.Profile = profile;
    cache?.LastRefresh = DateTime.UtcNow;

    return profile;
}

4. Plugin-Based Application

C#
public void Initialize(App app, IPluginContext pluginContext)
{
    app.Start();

    pluginContext?.State = PluginState.Initialized;
    pluginContext?.InitializedAt = DateTime.UtcNow;
}

Comparison: Before vs C# 14

AspectBefore C# 14C# 14 with ?.=
Null handlingManual checksBuilt-in
Code verbosityHigherLower
ReadabilityModerateHigh
Risk of null exceptionsHigherReduced
Intent clarityLess explicitClear

Decision Guide: When to Use or Avoid ?.=

QuestionUse ?.=Avoid ?.=
Is the object optional?YesNo
Is the assignment non-critical?YesNo
Is skipping the assignment acceptable?YesNo
Does null indicate a valid state?YesNo
Is this logging, telemetry, caching, UI state?YesNo
Is this core business logic?NoYes

When to Use Null-Conditional Assignment

When Not to Use Null-Conditional Assignment

Example to avoid

C#
order?.Status = OrderStatus.Completed;

Performance Considerations

Benefits Summary

Conclusion

Null-conditional assignment in C# 14 provides a concise and expressive way to handle optional object updates. It reduces repetitive null checks while maintaining performance and clarity. When used for auxiliary and defensive updates, it leads to cleaner, safer, and more maintainable code. Used thoughtfully, ?.= becomes a valuable addition to modern C# development.

Happy Coding!