19 November 2026

C# 15 Closed Hierarchies for Beginners: Student Enrollment States

  Asp.Net CS By Example       19 November 2026

A student application might be received, approved, or rejected. If every possible state has its own record, how can the compiler help you handle them all? C# 15 closed hierarchies let a base class list a fixed set of direct child types within one assembly. This beginner example uses that rule to describe enrollment states.

One base type, three direct cases

EnrollmentState (closed) → Applied / Approved / Rejected
All direct cases are declared in the same assembly.

What does closed mean?

A closed class is an abstract base class whose direct subclasses must be declared in the same assembly. The compiler can see the direct cases and recognize a switch expression that handles each one. The word does not mean that every child is automatically sealed. In this example, each child is also marked sealed so another class cannot extend it.

Create the console example

Install the .NET 11 SDK, run dotnet new console -n EnrollmentStates -f net11.0, and open that folder. The .NET 11 target uses C# 15 by default. Replace Program.cs with:

EnrollmentState[] states =
[
    new Applied(),
    new Approved(101),
    new Rejected("Missing documents"),
];

foreach (EnrollmentState state in states)
    Console.WriteLine(Describe(state));

static string Describe(EnrollmentState state) => state switch
{
    Applied => "Application received",
    Approved approved => $"Approved student {approved.StudentId}",
    Rejected rejected => $"Rejected: {rejected.Reason}",
};

public closed record class EnrollmentState;
public sealed record class Applied : EnrollmentState;
public sealed record class Approved(int StudentId) : EnrollmentState;
public sealed record class Rejected(string Reason) : EnrollmentState;

Run dotnet run. Expected lines: Application received, Approved student 101, and Rejected: Missing documents.

Why is there no default switch arm?

EnrollmentState has three direct cases, and the switch handles all three. The compiler therefore considers this switch exhaustive. Add public sealed record class Waitlisted(int Position) : EnrollmentState; in the same project and the switch is no longer complete. Add a Waitlisted arm before running it again. If the input type is nullable, also handle null.

Closed hierarchy or union?

A closed hierarchy uses inheritance: each case derives from the closed base class. A C# 15 union type lists case types in a union declaration; the cases do not have to derive from a shared base class. Both can help make switches complete, but their type relationships are different.

Practice

  1. Add the Waitlisted state and print its position.
  2. Remove one switch arm and read the compiler warning, then put it back.
  3. Try to define a direct child of EnrollmentState in another project. Explain why the compiler rejects it.

Continue with the C# learning path. For the exact language rules, see Microsoft's closed modifier reference and C# 15 guide.

logoblog

Thanks for reading C# 15 Closed Hierarchies for Beginners: Student Enrollment States

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.