skip to content

In C# 14, what happens to `profile?.DisplayName = BuildDisplayName();` when `profile` is null, and which forms does the compiler still reject?

level: seniorimportance: nice to knowfreq 24%

answer

  1. new in C# 14
  2. right side waits for the receiver
  3. compound yes, ++ no
  4. still not a variable

basics

~10 s

Nothing happens: since C# 14, ?. on the left of an assignment skips the whole assignment, and BuildDisplayName() is not even called. Compound operators work; ++, -- and ref uses are still rejected.

solid answer

~30 s

C# 14 allows `?.` and `?[]` as assignment targets, and the right-hand side is evaluated only when the receiver is non-null: `profile?.DisplayName = BuildDisplayName();` behaves like `if (profile is not null) profile.DisplayName = BuildDisplayName();` with `profile` read once. All compound assignments work, including `+=` and `??=`, and chains like `a?.b?.c = d` assign only when every link exists. The compiler still rejects `profile?.Visits++` (CS1059), passing `ref profile?.Visits` (CS1510) and a receiver that is a struct (CS0023 or CS0131). Used as a value, the expression is nullable: `int? stored = profile?.Visits = 10;`. With `LangVersion` 13 it is error CS9260.

code

csharp · 30 lines
csharp
Profile? signedOut = null;
Profile? signedIn = new Profile();

signedOut?.DisplayName = BuildDisplayName("signed out");  // receiver null: nothing runs
signedIn?.DisplayName = BuildDisplayName("signed in");    // prints, then assigns
signedOut?.Visits += CountVisit();                        // compound assignment: skipped too

string[]? recentSlots = null;
recentSlots?[0] = BuildDisplayName("slot");               // ?[] works the same way

int? stored = signedIn?.Visits = 10;                      // used as a value: int?, not int
Console.WriteLine(stored);                                // 10

static string BuildDisplayName(string who)
{
    Console.WriteLine($"building name for {who}");
    return who.ToUpperInvariant();
}

static int CountVisit()
{
    Console.WriteLine("counting a visit");
    return 1;
}

class Profile
{
    public string? DisplayName { get; set; }
    public int Visits { get; set; }
}

go deeper

for a junior

Recall that C# 14 lets you write obj?.Prop = value, and that it does nothing when obj is null.

for a middle

Explain that the right side is evaluated only for a non-null receiver, and which operators are allowed or rejected.

for a senior

Judge refactorings that move side effects into the right-hand side, and where silent skipping hides a real bug.

for a principal

Weigh adopting the C# 14 form in shared code against older toolchains that still need the explicit if form.

## The C# 14 feature Before C# 14 a null-conditional access could only be read. Assigning through one meant spelling out the check: ```csharp if (profile is not null) { profile.DisplayName = BuildDisplayName(); } ``` C# 14 (shipped with .NET 10) adds **null-conditional assignment**: `?.` and `?[]` may appear on the left of `=` and of compound assignment operators. ```csharp profile?.DisplayName = BuildDisplayName(); recentSlots?[0] = BuildDisplayName(); ``` With an older language version, for example `LangVersion` 13, the compiler reports error CS9260: "Feature 'null conditional assignment' is not available in C# 13.0. Please use language version 14.0 or greater." ## The right-hand side runs only when the receiver exists The design rule that interviewers probe: **the right side of the assignment is evaluated only when the receiver is non-null**. The C# 14 proposal specifies `P?.A = B` in a statement as `if (P is not null) P.A = B;`, with `P` evaluated once. So: - when `profile` is null, `BuildDisplayName()` is **not called** — no work, no side effects, no exceptions from it; - when `profile` is non-null, `BuildDisplayName()` runs and its result is assigned through the setter; - the current value of `profile.DisplayName` plays no part in deciding what runs. The receiver expression itself is evaluated exactly once and first. In `GetProfile()?.DisplayName = BuildDisplayName();`, `GetProfile()` runs once; if it returns null, `BuildDisplayName()` does not run; if it returns a profile, the name is built and assigned to that same instance, with no second call to `GetProfile()`. This matters when refactoring. Converting `if (profile is not null) profile.DisplayName = Build();` to `profile?.DisplayName = Build();` preserves behaviour. Converting `var name = Build(); profile?.DisplayName = name;` into the one-liner does not, if `Build()` had side effects that were meant to happen regardless — logging, counters, a cache warm-up. ## What is allowed - Simple assignment: `profile?.DisplayName = value;` - Element assignment: `recentSlots?[0] = value;` - Every compound assignment: `profile?.Visits += 1;`, `-=`, and so on. The proposal's own example attaches a handler with `c?.E += handler;`. - `??=` through a conditional access: `page?.Profile?.DisplayName ??= Build();` assigns only when `page` and `Profile` exist and `DisplayName` is null. - Chains: `a?.b?.c = d` assigns only when both `a` and `a.b` are non-null. ## What the compiler still rejects | Code | Diagnostic (SDK 10.0.401) | Why | |---|---|---| | `profile?.Visits++;` or `--profile?.Visits;` | CS1059 | increment and decrement were deliberately left out | | `Touch(ref profile?.Visits);` | CS1510 | a conditional access is still not a variable | | `s?.V = 1;` with `s` a non-nullable struct | CS0023 | `?` cannot be applied to a non-nullable value type | | `ns?.V = 1;` with `ns` a nullable struct | CS0131 | `ns.Value` is a copy, not a variable to assign into | The documentation summarizes the boundary: the feature does not classify a null-conditional expression as a variable, so it cannot be `ref`-assigned, assigned to a `ref` variable or passed as a `ref` or `out` argument. ## The value of the assignment expression An assignment is an expression, and so is a null-conditional one. When its value is used, its type is the nullable form of the assigned type, null when the receiver was null: 1. `int? stored = profile?.Visits = 10;` — `int?`, holding 10 or null. 2. `var name = profile?.DisplayName = "Ada";` — a possibly-null `string`. 3. In a generic type with an unconstrained `T`, using the value fails with CS8978 ("'T' cannot be made nullable"), while the same assignment as a plain statement compiles. ## When to use it - Optional collaborators: `profile?.LastSeen = DateTimeOffset.UtcNow;` on a page where a visitor may be signed out. - Optional UI or view-model objects that may not be built yet. - Avoid it when the null receiver is a bug. `?.` silently skips; if `profile` must exist at this point, a plain `.` that throws, or an explicit guard, reports the bug instead of hiding it. Older codebases and Unity projects compiled with an earlier C# version cannot use it, so in shared code the `if` form is still the portable spelling.

  • Why does `page?.Profile?.DisplayName ??= Build();` call `Build()` at most once per null name?
    The chain assigns only when `page` and `Profile` are non-null, and `??=` then evaluates `Build()` only when `DisplayName` is null. If any link is null, or the name is already set, `Build()` never runs. The proposal spells it as nested `if` checks on `a` and then on `a.b`.
  • Why can a struct-typed receiver not use null-conditional assignment?
    A non-nullable struct cannot be null, so `s?.V` is rejected outright (CS0023). For a nullable struct, `ns?.V` would assign into `ns.Value`, which is a copy rather than a variable, so the compiler reports CS0131. The feature targets reference-type receivers.

  • profile?.DisplayName = Build() still calls Build() when profile is null
  • Null-conditional assignment works with ++ and -- as well
  • A conditional access can now be passed as a ref argument
  • The assignment's value is the plain property type even when skipped
  • Null-conditional assignment has been in C# since version 6

Assumes C# 14 (.NET 10 SDK); null-conditional assignment does not exist in C# 13 or earlier, where the compiler reports CS9260.