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.

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”.

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.

Inspect the results without mixing denominators
| Fixture | Diagnostic | Build exit | Expected / executed |
|---|---|---|---|
| 2 cases + null | None | 0 | 3 / 3 |
| 3rd case missing | CS8509 | 0 | 3 / 4 |
| Missing + strict policy | CS8509 | 1 | Not executed |
| Catch-all branch | None | 0 | 2 / 4 |
| 3 cases + null | None | 0 | 4 / 4 |
| Nullable: no null arm | CS8655 | 0 | 3 / 4 |
| Nullable: null arm | None | 0 | 4 / 4 |
| Non-null cases: no null arm | None | 0 | 3 / 4 |
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 StartedThe 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
- Compiler configuration and diagnostic ledger (CSV)
- All 27 observed runtime results (CSV)
- Version and environment summary (CSV)
- Blank consumer expectation ledger (CSV)
- Source projects, runner and retained logs (ZIP)
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.
