---
title: "Closed class hierarchies in C# 15"
url: https://daily.dev/posts/closed-class-hierarchies-in-c-15-ggrvkhfpw
source_url: https://www.damirscorner.com/blog/posts/20260814-ClosedClassHierarchiesInCs15.html
type: article
source: "Damir's Corner"
published: 2026-08-14T06:43:56.750Z
updated: 2026-08-18T08:03:25.884Z
tags: [".net", "c#", "language-design"]
reading_time: 5
upvotes: 0
comments: 0
language: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Closed class hierarchies in C# 15

**[Damir's Corner](https://daily.dev/sources/damirscorner)** · 5 min read · 0 upvotes · 0 comments

## Summary

C# 15, previewed in .NET 11, introduces a new 'closed' keyword that lets developers mark a base class or record as closed to derivation from other assemblies. This makes switch expressions over such class hierarchies exhaustive without needing a discard pattern, so the compiler warns immediately when a new derived class is added but not handled. The post walks through open vs. closed hierarchies, existing workarounds using internal/private protected constructors, and how closed interacts with switch exhaustiveness checks, including edge cases like deriving from a derived class. The feature is still in preview and testable via .NET 11 preview 6 by setting LangVersion to preview.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.damirscorner.com/blog/posts/20260814-ClosedClassHierarchiesInCs15.html>

## Questions this post answers

### What does the new 'closed' keyword do in C# 15?

The closed keyword, introduced in C# 15, can be applied to a base class or record to prevent other assemblies from deriving new classes from it. This turns the class hierarchy into a closed class hierarchy, so switch expressions over its derived types are considered exhaustive by the compiler without needing a discard (_) pattern to suppress the CS8509 warning.

_daily.dev surfaces language updates like this for developers tracking new C# releases before they upgrade._

### How can I prevent other assemblies from deriving from my C# base class before C# 15 adds the closed keyword?

Before C# 15, you can make the base class constructor inaccessible outside the assembly using internal (accessible within the assembly) or private protected (accessible only from derived classes). This blocks external derivation, causing error CS7036 when attempted, but the compiler still requires a discard pattern in switch expressions since it doesn't recognize the hierarchy as formally closed.

_Developers weighing access-modifier workarounds against new language features can track patterns like this on daily.dev._

### Why does the C# compiler show warning CS8509 for a switch expression over an abstract record with multiple derived types?

CS8509 appears because the compiler cannot guarantee no additional classes derive from the base type at runtime, since another assembly compiled without knowledge of the switch expression could add new derived classes to what is called an open class hierarchy. Adding a discard pattern (_ => ...) or marking the base as closed in C# 15 resolves the warning.

_daily.dev helps developers stay current on compiler warnings and language changes as they debug exhaustiveness issues._

## Similar posts on daily.dev

- [Closed class hierarchies](https://daily.dev/posts/closed-class-hierarchies-xmmjyy63z) · .NET Escapades · 15 upvotes · 0 comments

---

Tags: [#.net](https://daily.dev/tags/.net), [#c#](https://daily.dev/tags/c#), [#language-design](https://daily.dev/tags/language-design)

[View this post on daily.dev](https://daily.dev/posts/closed-class-hierarchies-in-c-15-ggrvkhfpw)
