C# 15 Union Types: Test Missing Cases and Null States

C# 15 union types can make a changing outcome model easier to inspect. However, a compiler warning, a failed build and a correct response are three different results. Our eight-fixture library lookup experiment makes that distinction visible.

We compiled each fixture with the final .NET SDK 11.0.100. Seven builds succeeded and one failed under a strict warning policy. Across the successful builds, 22 of 27 designed inputs returned the expected answer. The other five exposed an omitted branch or an unsuitable fallback. These are teaching results, not estimates of production defect rates.

Why test C# 15 union types now?

Microsoft released .NET 11 on 10 November 2026, including C# 15 union types. That is the timely hook. Our question is narrower: what happens when a fictional book lookup gains a third possible outcome?

The language can express a fixed set of alternatives. In this exercise, a lookup can find a book, find no matching book, or be temporarily unavailable. A developer must still decide what each outcome means for the reader. A compiler cannot choose the right retry message for a library service.

The lookup contract we tested

First, we fixed the expected responses before running the code. Found returns the title, NotFound returns “absent”, and TemporarilyUnavailable returns “retry”. We also agreed that empty default contents should return “empty”. That last rule is our application choice, not a universal recommendation for every union.

public record class Found(string Title);
public record class NotFound;
public record class TemporarilyUnavailable;
public union Lookup(Found, NotFound, TemporarilyUnavailable);

The baseline has only Found and NotFound. Later fixtures add the third type, change the switch arms, or change the warning policy. Each fixture lives in a separate project, so the strict build cannot accidentally run an older successful binary.

Three evidence questions: the type contract defines alternatives, compiler policy reports omissions, and business tests compare actual responses with agreed meanings.
Type choices and compiler feedback both feed into business tests; neither replaces a declared expected response.

A missing case warns; a strict policy stops the build

Next, we added TemporarilyUnavailable while keeping the earlier switch arms. The normal build reported CS8509 and still exited successfully. When we supplied the unavailable input, the switch threw a SwitchExpressionException instead of returning “retry”.

We then compiled the same missing-case design with TreatWarningsAsErrors enabled. CS8509 became an error and the build exited with code 1. We did not execute that failed fixture. Its zero runtime inputs mean “not run”, not “all tests passed”.

Eight fixtures: missing added case produces CS8509 as warning or error depending on policy; nullable missing-null fixture produces CS8655; five other fixtures have no distinct diagnostic.
Diagnostics are counted once per source location and identifier, excluding repeated summary lines. Warning policy changes whether the missing-case build succeeds.

A catch-all can silence feedback and hide intent

The discard fixture used a final underscore arm returning “other”. It compiled without a diagnostic. Yet only two of its four inputs matched our agreed answers: both the unavailable case and default contents reached that generic response.

This is why an apparently tidy fallback deserves a concrete test. It might be appropriate when every remaining case truly shares one meaning. In our contract, retry and empty are different responses. Treating them alike loses information that the reader needs.

Default contents need a separate test

Our explicit three-case fixture included a null arm and passed all four inputs. Removing that arm from a nullable-case variant produced CS8655, and the default input threw. Restoring the null arm made all four inputs match again.

The final fixture is especially useful for review: all declared case types were non-nullable, every named case had an arm, and the build emitted no diagnostic. Nevertheless, a default-initialised value reached the switch and threw. This locally observed result is a reason to include default inputs in the test set.

The current Microsoft union reference explains the distinction between tracked null state and runtime default contents. An explicit null arm can preserve missing-case feedback, while an unrestricted discard accepts later cases too. Check those semantics against your installed compiler when reproducing the lab.

Expected versus mismatched results across seven executable fixtures: 3/3, 3/4, 2/4, 4/4, 3/4, 4/4 and 3/4. The strict failed build is excluded.
The 27 executed inputs include repeated teaching cases across different configurations. These counts compare the fixtures; they do not measure application reliability.

Inspect the results without mixing denominators

FixtureDiagnosticBuild exitExpected / executed
2 cases + nullNone03 / 3
3rd case missingCS850903 / 4
Missing + strict policyCS85091Not executed
Catch-all branchNone02 / 4
3 cases + nullNone04 / 4
Nullable: no null armCS865503 / 4
Nullable: null armNone04 / 4
Non-null cases: no null armNone03 / 4
Author-run synthetic fixtures, 15 November 2026. Each row is a separate compiler configuration.

There are eight builds, seven executable fixtures and 27 executed inputs. Those denominators answer different questions. For example, counting three diagnostic-bearing builds does not reveal how many distinct production consumers would need changing. We intentionally tested one small rendering function in several controlled forms.

Run the lab in your own disposable folder

Download the source pack and install the recorded final SDK from Microsoft’s .NET download page. Inspect the source, choose a writable local folder, then build a fixture with dotnet build. Run its Lab.dll only after that build succeeds. There are no external package dependencies or real library records.

Tech Consulting Tailored to Your Coding Journey

Get expert guidance in coding with a personalized consultation. Receive unique, actionable insights delivered in 1-3 business days.

Get Started

The included Python runner is the automation used for this study. Set its SDK path to your own dotnet executable before reproducing it. Its fixture folders contain global.json pins, project files, C# source and retained build/runtime logs. Keep your new logs separately so you can compare versions honestly.

Use the blank consumer ledger to add another function with its own expected responses. For example, a display view might show “try again”, while an API response might require an explicitly agreed status. Build feedback can identify an unhandled type; your tests establish whether the branch meets that consumer’s contract.

Download the evidence and planning tools

Method, limits and sources

We ran the lab on macOS ARM64 with SDK 11.0.100, target net11.0, language version 15.0 and nullable analysis enabled. Package sources were cleared and no external packages were required. The runner records build exit codes, distinct source diagnostics and each actual response. Runtime exceptions count as mismatches against the predefined answers.

The eight configurations are a deliberate convenience design, not a random sample. We did not measure execution speed, developer productivity, defect reduction, every union feature or compatibility with other SDKs. Repeated inputs across fixtures are not independent users. Passing these cases does not establish correct business rules beyond the small declared contract.

For related practice, explore our Programming Languages guides and Software Development Best Practices. The reusable habit is to keep allowed outcomes, compiler policy and expected behaviour in separate records.