# What's new in C# 15: Closed hierarchies

A package is packed, shipped or delivered, and nothing else. C# 15 lets a base class say "this family is complete", so the compiler can check every switch for you.

By Gregor Dzierzon · Published November 14, 2026

![Codey stands at a shipping counter beside three parcels stamped Packed, Shipped and Delivered](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/11/ebd9a1d2eacf4fda9ceeb67e242c204a.webp)

> **What's new in C# 15**
>
> 1. [The release: .NET 11, LTS and STS](https://www.ask-codey.com/blog/whats-new-in-csharp-15)
> 2. [Union types](https://www.ask-codey.com/blog/whats-new-in-csharp-15-union-types)
> 3. [Labeled `break` and `continue`](https://www.ask-codey.com/blog/whats-new-in-csharp-15-labeled-break-and-continue)
> 4. **Closed hierarchies**
> 5. Collection expression arguments
> 6. Extension indexers
> 7. For library authors: static interface members on older runtimes, and memory safety
>
> [All posts in this series](https://www.ask-codey.com/blog?tag=csharp-15)

## Closed hierarchies

Think about tracking a package you ordered. At any moment it's in one of a few states: **packed** and waiting for the truck, **shipped** with a tracking number, or **delivered** on a certain day. Every state is a kind of shipment, and each one has its own details.

That's a natural fit for inheritance: a base class for "a shipment", and a class for each state that derives from it.

```csharp
public abstract record class Shipment;
public record class Packed : Shipment;
public record class Shipped(string TrackingNumber) : Shipment;
public record class Delivered(DateOnly On) : Shipment;
```

### The problem: the family is never finished

![A Shipment family tree with Packed, Shipped and Delivered under it, and an open gate with a parcel marked ? walking in; Codey says the compiler can't know these are all of them](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/11/2410e53f9824423a8784d28574e80464.webp)

Now the order page shows a line for each state:

```csharp
string status = shipment switch
{
    Packed => "Packed and waiting for the truck",
    Shipped shipped => $"On its way: {shipped.TrackingNumber}",
    Delivered delivered => $"Delivered on {delivered.On:MMM d}",
    _ => "Unknown",
};
```

You know there are three states. The compiler doesn't. Any code, anywhere, could write a fourth class that derives from `Shipment`, so the compiler warns you until you add the `_` "anything else" arm. And once that arm is there, it hides your own mistakes: add a `Returned` state next month, forget to update this `switch`, and the page quietly shows "Unknown" to a customer whose package is coming back.

If you read [the union types post](https://www.ask-codey.com/blog/whats-new-in-csharp-15-union-types), this is the same problem. Unions solve it for types that have nothing in common. Closed hierarchies solve it for a family that really does share a base class.

### The C# 15 answer: `closed`

Mark the base class **`closed`**:

```csharp
public closed record class Shipment;
public record class Packed : Shipment;
public record class Shipped(string TrackingNumber) : Shipment;
public record class Delivered(DateOnly On) : Shipment;
```

`closed` means: **every class that derives directly from this one must be in the same project.** Code in other projects can use a `Shipment`, but can't add a new kind of it. So the compiler can see the whole family, and the "anything else" arm goes away:

```csharp
string status = shipment switch
{
    Packed => "Packed and waiting for the truck",
    Shipped shipped => $"On its way: {shipped.TrackingNumber}",
    Delivered delivered => $"Delivered on {delivered.On:MMM d}",
};
```

Now add that `Returned` state:

```csharp
public record class Returned(string Reason) : Shipment;
```

and the compiler points at every `switch` that needs it:

```text
warning CS8509: The switch expression does not handle all possible values of its input type
(it is not exhaustive). For example, the pattern 'Returned' is not covered.
```

No customer sees "Unknown". You see the warning first.

![The Shipment family stands inside a fenced yard with a gate sign reading closed: new kinds only in this project; a new parcel, Returned, inside the fence has a tag reading Not handled yet, and Codey points at it](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/11/b145a6d26580442bbff8bfc1deb40c70.webp)

### Good to know

- **"Same project" is the plain version.** Microsoft's wording is the same *assembly*, the `.dll` file one project builds.
- **A closed class is automatically abstract.** You can't create a plain `Shipment`, only one of its kinds, which is what you wanted anyway.
- **Only the first level is closed.** A class that derives from `Shipped` can live anywhere, unless you seal `Shipped`.
- **It works for classes and record classes,** not for structs or interfaces.

### Union or closed hierarchy?

![On the left, a family tree, Shipment with Packed, Shipped and Delivered, labeled One family: closed hierarchy; on the right, three separate cards, Paid, Declined and Pending, in a ring labeled Separate types: union; Codey stands between them](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/11/79aa2204df6f454497d3a34368304d28.webp)

Both give you a `switch` the compiler can check. The difference is whether the cases belong together:

- **Closed hierarchy:** the cases are one family that shares a base class, and maybe shared data or methods. Packed, shipped and delivered are all *shipments*.
- **Union:** the cases are separate types that only meet in one place. A payment result can be a `Paid`, a `Declined` or a `Pending`, and those records are useful on their own.

**On one page:** [the closed hierarchies cheat sheet](https://www.ask-codey.com/cheat-sheets/csharp-15-closed-hierarchies-cheat-sheet), to keep or to share.

## Sources

- Microsoft, [closed (C# reference)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/closed), Microsoft Learn: the rules for closed classes.
- Microsoft, [What's new in C# 15](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-15), Microsoft Learn: every C# 15 feature, with examples.

Tags: [csharp](https://www.ask-codey.com/blog?tag=csharp), [csharp-15](https://www.ask-codey.com/blog?tag=csharp-15), [dotnet](https://www.ask-codey.com/blog?tag=dotnet), [whats-new](https://www.ask-codey.com/blog?tag=whats-new)

## Glossary subjects

- [C#](https://www.ask-codey.com/glossary/subjects/c-sharp)

## More posts

- Previous: [What's new in C# 15: Labeled break and continue](https://www.ask-codey.com/blog/whats-new-in-csharp-15-labeled-break-and-continue)
- Next: [What's new in C# 15: Collection expression arguments](https://www.ask-codey.com/blog/whats-new-in-csharp-15-collection-expression-arguments)

Source: https://www.ask-codey.com/blog/whats-new-in-csharp-15-closed-hierarchies
