C# 15 ships union types, and the compiler only warns when an agent forgets a case
The new exhaustiveness check is real, but it's a warning by default, a discard arm switches it off, and default(Union) has a surprise. One line of project config fixes the first.

C# 15 shipped on 10 November 2026 with .NET 11, and its headline feature fixes a problem that every agent-written pull request in a C# codebase has: a new case gets added in one place and silently ignored in the rest. A union declares a closed set of case types, and a switch over it is checked for exhaustiveness without a catch-all arm. Microsoft’s announcement
says C# 15 is the default language version for net11.0, and the C# 15 page
says that apart from the memory safety work, none of it needs LangVersion set to preview.
The catch is that the check is a compiler warning, and a warning doesn’t stop an agent that sees Build succeeded. Here’s how the feature works, where it leaks, and the one setting that makes it a gate.
What shipped
A union lists its case types. The cases don’t have to be related, and the compiler gives each one an implicit conversion:
public record Product(string Sku, decimal Price);
public record ProductNotFound(string Sku);
public union ProductLookup(Product, ProductNotFound);
public static string Describe(ProductLookup result) => result switch
{
Product p => $"{p.Sku} costs {p.Price:0.00}",
ProductNotFound n => $"no product {n.Sku}",
};
That compiles and runs on the 11.0.100 SDK with a net11.0 target and nothing else configured. Add a third case type and every switch that doesn’t handle it is reported. Add public record Discontinued(string Sku, DateOnly Since); to the union and the build prints:
Program.cs(9,67): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern 'Discontinued' is not covered.
Closed hierarchies do the same job for class trees. Mark the base closed, and its direct descendants must live in the same assembly, so a switch that covers them needs no default arm:
public closed record class GateState;
public record class Closed : GateState;
public record class Open(float Percent) : GateState;
public record class Stuck(string Reason) : GateState;
A switch that handles Closed and Open but not Stuck gets the same CS8509. Per the docs, closed is implicitly abstract, and the guarantee covers direct descendants only: a non-closed child can still be derived from in other assemblies unless it’s marked closed too.
The same release adds a few smaller things. Collection expressions can take constructor arguments through a leading with(...) element, for example HashSet<string> set = [with(StringComparer.OrdinalIgnoreCase), "Hello", "HELLO"];, which holds one element when run. There are also extension indexers and labeled break and continue.
Why it matters for agent-written code
Before this, C# had no built-in way to say “a closed set of outcomes” except an enum, and an enum carries no data. Hand-rolled class hierarchies and visitor patterns gave the compiler nothing to check a switch against. The enum version of this problem needed a build setting, a test and a script.
Unions and closed hierarchies move that knowledge into the type. A result type that is Product or ProductNotFound or Discontinued stops being a convention in a comment. When an agent adds Discontinued, the compiler lists every consumer that now has a hole. That’s a much better review aid than a diff that shows only the lines the agent chose to touch.
Leak one: it’s only a warning
CS8509 is a warning at the default settings, so the build in the example above succeeded and the program ran. An agent that reads the final line of the build output and sees success has no reason to look further. Promote that one diagnostic to an error in the project file:
<PropertyGroup>
<TargetFramework>net11.0</TargetFramework>
<WarningsAsErrors>CS8509</WarningsAsErrors>
</PropertyGroup>
With that line the same build prints error CS8509 and fails, which is text the agent can act on. TreatWarningsAsErrors does it too, but it fails the build on every other warning as well, so a targeted list is the easier change to get through review.
Leak two: the discard arm
Exhaustiveness is satisfied by anything that matches everything. When the build goes red on a new case, the cheapest fix is to add _ => "unknown" to the switch. Run that and the build is green, the output for the new case is unknown, and the diagnostic is gone for good.
Microsoft’s union post notes that var and _ patterns apply to the whole union, so they match whatever case is added later. Nothing in the compiler distinguishes a deliberate catch-all from a patched-over hole.
This one has no compiler switch. It’s a review rule, and a cheap one to automate: a grep for _ => in the diff, flagged when the switched type is a union or a closed hierarchy. Put the rule in the file the agent reads:
When a switch over a union or closed type stops compiling because of a new case, handle the new case explicitly with its own arm. Never add a discard or var arm to make the error go away.
That’s a request, not enforcement, so keep the grep as well.
Leak three: the default union
A union is a struct. Per the post that introduced the feature, the compiler generates a struct with a constructor for each case type and a Value property of type object?, and value-type cases are boxed. The struct’s default value therefore holds a null Value, which matches none of the case arms.
The documentation shows a null => "no pet" arm for this. Without one, the switch compiles with no warning and throws at run time. With the two-case lookup above:
ProductLookup empty = default;
Console.WriteLine(Handler.Describe(empty));
SwitchExpressionException
Anywhere a union sits in a field, an array or a freshly allocated struct without being assigned, it can be default. Decide once whether that state is legal. If it isn’t, validate at the boundary and don’t give every consumer a null arm. If it is, give it a case type of its own (a NotLoaded record) so the switch says what it means.
A caution on the documentation
Microsoft’s union types post
still carries instructions from .NET 11 Preview 2: install the preview SDK, set LangVersion to preview, and declare UnionAttribute and IUnion yourself in System.Runtime.CompilerServices. On the released SDK none of that was needed for the examples above. An agent, or a developer, that follows the older post may add scaffolding the compiler no longer wants, and the post also says member providers aren’t implemented yet. Treat the C# 15 page on Learn, dated 10 November 2026, as the reference.
The posts and docs don’t say how unions interact with serialisation or EF Core, so don’t assume System.Text.Json will round-trip a union without testing it. That’s the first thing to check before replacing a DTO hierarchy.
Upgrading is not a version bump
.NET 11 is Standard Term Support. Microsoft’s release metadata gives 14 November 2028 as its end-of-support date, which fits the announcement’s two years. The same post says .NET 8 and .NET 9 reach end of support on 10 November 2026, so anyone still on those is already past it, and .NET 10 remains supported as an LTS option.
The announcement warns that installing a new SDK doesn’t retarget an app. The steps it lists are to update global.json if the repo pins an SDK, change the target framework, update packages and workloads, read the breaking-changes pages, then run tests under a representative workload. Package feeds are already moving. An earlier Tokenise post
found that the newest Microsoft.AspNetCore.Mvc.Testing on the feed targeted net11.0 and refused to restore into a net10.0 project, so a project that stays on net10.0 should pin a 10.* version.
Tokenise’s own C# examples stay on net10.0 until the maintainers say otherwise. The union examples here ran on an 11.0.100 SDK and need net11.0.
Try this
Take one switch over a result type in a codebase that has moved to net11.0, convert the type to a union, and add <WarningsAsErrors>CS8509</WarningsAsErrors> to the project. Then add a case and see which consumers light up. If the list is longer than the diff you’d have reviewed by hand, the feature has already paid for itself.