Read or download the complete SDK project, then follow its setup on your computer. The separate C# companion practice below runs in SharpRune’s .NET 9 console environment.
Abstract base here, closed base in C# 15
abstract record GateState- Any assembly could add a descendant
- A
_ => throwarm is still required
public closed record GateState- Direct descendants only in this assembly
- Three arms are exhaustive; a missing one fails the build
- Closed is implicitly abstract; leaves are sealed
Both versions print shut, opening:40 and open. Only the closed external hierarchy lets the compiler prove that three arms cover every direct descendant.
The concept and environment
C# 15 adds the closed modifier for classes, including record classes. A closed class can be derived from directly only inside its own assembly, so the compiler knows every direct descendant, and a switch expression that handles each of them is exhaustive without a default arm. Unlike a union, the alternatives share a base type and can share members. The external .NET 11 project declares a closed GateState. SharpRune's .NET 9 companion uses an abstract record and must keep a throwing discard arm, because nothing stops another descendant from appearing.
Walk through the portable program
The companion builds an array with three states: Shut, Opening at forty percent, and Open. Describe matches each record and uses a positional pattern to read the percentage from Opening. Output is shut, opening:40 and open. The fourth arm, a discard that throws ArgumentOutOfRangeException, never runs in this program. It exists because an abstract base type is open: any class in any referencing assembly could derive from it, and the compiler cannot prove that three arms cover every possible value it might receive. The guard is honest, but it reports a forgotten case only when that case finally reaches this code path at run time.
Compile the genuine external feature
In the external project, GateState.cs declares public closed record GateState and three sealed records that derive from it. Describe has exactly three arms and no discard. The project file treats CS8509 as an error, so removing the Open arm stops the build with a message that names the missing pattern. A closed class is implicitly abstract, so creating a GateState directly is rejected with CS0144. The project targets net11.0 and stable language version 15.0, and its output matches the portable companion line for line.
Pitfalls and limits
Closure is not transitive. Only the direct descendants of a closed class are fixed; a descendant that is neither sealed nor closed can itself be derived from in another assembly, which is why this example seals each leaf. The modifier cannot be combined with sealed, static or an explicit abstract modifier. A switch over a nullable GateState must also handle null. Inside the declaring assembly, adding a new direct descendant is still allowed, and it is a source-breaking change for every exhaustive switch over the base type.
Practice one: isolate a contract
First, add a fourth record, Jammed, deriving from GateState, without changing Describe. Predict which version fails to build. Hint: the external project reports CS8509 for Jammed, while the companion builds and would throw ArgumentOutOfRangeException only at run time. Add the missing arm to both versions. Then remove the sealed modifier from Opening and decide, in writing, whether another assembly should be allowed to derive from it; the closed modifier on the base type alone does not answer that question for you. Record that decision next to the type declaration.
Practice two: challenge an assumption
For another exercise, change Describe to accept a nullable GateState and pass null in a new call. Predict the warning and the arm you need. Hint: null becomes another possible value, so add an explicit null arm rather than a discard, which would also silence future missing cases. Then compare the closed hierarchy with the union lesson that precedes it: write down which representation you would choose if every state needed a shared Duration property, and which you would choose for unrelated result types.
Review the resulting contract
Review a closed hierarchy by asking who may add a direct descendant and which switches must change when one is added. The modifier moves that answer from convention to the compiler, and exhaustiveness turns a missing case into a build error. Seal or close the leaves when their own descendants should be fixed too. The external project demonstrates the genuine binding. The portable companion shows the same state rules with the older open hierarchy, where only a run-time guard catches an unexpected subtype that arrives later.
C# 15 closed class hierarchies lab
Set up the SDK project
- Save every listed file at its exact relative path in a fresh directory.
- Install a .NET 11 SDK supporting stable C# 15; check dotnet --version.
- Run dotnet run --project FeatureLab.csproj. Keep this external code separate from the .NET 9 playground.
Download complete SDK project ↗
Project files
Program.cs
Download file ↗GateState[] states = [new Shut(), new Opening(40), new Open()];
foreach (GateState state in states) Console.WriteLine(Describe(state));
static string Describe(GateState state) => state switch
{
Shut => "shut",
Opening(var percent) => $"opening:{percent}",
Open => "open",
};
GateState.cs
Download file ↗public closed record GateState;
public sealed record Shut : GateState;
public sealed record Opening(int Percent) : GateState;
public sealed record Open : GateState;
FeatureLab.csproj
Download file ↗<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net11.0</TargetFramework>
<LangVersion>15.0</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<WarningsAsErrors>CS8509</WarningsAsErrors>
</PropertyGroup>
</Project>
Expected result
Expected console output, in this order: shut opening:40 open The portable companion compares the same small rule using older syntax; it does not compile or emulate the new feature.
Use the listed SDK and dependencies for this project. Open C# practice runs the separate portable exercise.
C# companion practice
using System;
GateState[] states = [new Shut(), new Opening(40), new Open()];
foreach (GateState state in states) Console.WriteLine(Describe(state));
static string Describe(GateState state) => state switch
{
Shut => "shut",
Opening(var percent) => $"opening:{percent}",
Open => "open",
_ => throw new ArgumentOutOfRangeException(nameof(state)),
};
abstract record GateState;
sealed record Shut : GateState;
sealed record Opening(int Percent) : GateState;
sealed record Open : GateState;
C# practice output
shut opening:40 open
An abstract base is open, so the companion keeps a throwing discard. The closed external base lets three arms be exhaustive.