Defining Closed Types In C# & .NET
[C#, .NET]
Whenever you use inheritance in your code, you are opening yourself up for some very subtle problems downstream.
Take this type:
public abstract record Agent
{
public required string FullNames { get; init; }
public required DateOnly DateOfBirth { get; init; }
}
We derive an OfficeAgent and a FieldAgent from this:
public record OfficeAgent : Agent
{
}
public record FieldAgent : Agent
{
public required string Posting { get; init; }
}
We then have a simple program that given an Agent, prints some information pertinent to the type:
var officeAgent = new OfficeAgent
{
DateOfBirth = new DateOnly(1980, 1, 1), FullNames = "Eve MoneyPenny"
};
var fieldAgent = new FieldAgent
{
DateOfBirth = new DateOnly(1950, 1, 1), FullNames = "James Bond", Posting = "Prague",
};
Agent[] agents = [officeAgent, fieldAgent];
foreach (var agent in agents)
{
var greeting = agent switch
{
FieldAgent s => $"Hi {s.FullNames}, is a field agent, posted to {s.Posting}",
OfficeAgent => $"Hello {agent.FullNames}, is an office agent",
};
Console.WriteLine(greeting);
}
This will print the following:

So far so good.
However there is a subtle problem here:
var greeting = agent switch
{
FieldAgent s => $"Hi {s.FullNames}, is a field agent, posted to {s.Posting}",
OfficeAgent => $"Hello {agent.FullNames}, is an office agent",
};
Console.WriteLine(greeting);
This presupposes that the Agent hierarchy is fixed.
If we add this to our type system in the main Program:
public record SpecialistAgent : Agent
{
public required string Speciality { get; init; }
}
And update our program like so:
var officeAgent = new OfficeAgent
{
DateOfBirth = new DateOnly(1980, 1, 1), FullNames = "Eve MoneyPenny"
};
var fieldAgent = new FieldAgent
{
DateOfBirth = new DateOnly(1950, 1, 1), FullNames = "James Bond", Posting = "Prague",
};
var specializedAgent = new SpecialistAgent()
{
DateOfBirth = new DateOnly(1950, 1, 1), FullNames = "James Bond", Speciality = "Communications",
};
Agent[] agents = [officeAgent, fieldAgent, specializedAgent];
foreach (var agent in agents)
{
var greeting = agent switch
{
FieldAgent s => $"Hi {s.FullNames}, is a field agent, posted to {s.Posting}",
Agent => $"Hello {agent.FullNames}, is an office agent",
};
Console.WriteLine(greeting);
}
It will STILL print the following:

This is because the switch statement is not exhaustive as the compiler cannot guarantee this.
The solution to this is the closed modifier.
We mark our base class as follows:
public closed record Agent
{
public required string FullNames { get; init; }
public required DateOnly DateOfBirth { get; init; }
}
If we now try and derive from Agent in a class that is in the main Program (not the same Assembly as Agent) we get a compiler warning:
ClosedProgram net11.0 failed with 1 error(s) (0.2s)
/Users/rad/Projects/BlogCode/ClosedHierarchy/ClosedProgram/SpecialistAgent.cs(3,15): error CS9382: 'SpecialistAgent': cannot use a closed type 'Agent' from another assembly as a base type.
Build failed with 1 error(s) in 0.7s

This means that any class deriving from Agent must be defined in the original assembly, and thus the compiler can detect if there is a derivative missing in the switch.
ClosedProgram net11.0 succeeded with 1 warning(s) (0.2s) → ClosedProgram/bin/Debug/net11.0/ClosedProgram.dll
/Users/rad/Projects/BlogCode/ClosedHierarchy/ClosedProgram/Program.cs(23,26): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern 'ClosedHierarchy.v2.SpecialistAgent' is not covered.

If you try to derive from a closed class in another assembly, you will get a comple time error:

This is how the compiler is able to make that type guarantee.
TLDR
The closed keyword allows you to control which assemblies can derive from your base types.
The code is in my GitHub.
Happy hacking!