How to Stop a Class from Being Inherited with sealed in C#
sealed class Bicycle : Vehicle makes Bicycle the end of the line: it inherits from Vehicle, but no class can inherit from Bicycle. The compiler enforces it, so a class that tries doesn't build.
Microsoft's reference: When you apply the sealed modifier to a class, it prevents other classes from inheriting from that class. Nothing else changes: you still create objects from a sealed class, set and read their properties, and call their methods.
Syntax
sealed class <Name>
{
// members
}
sealed class <Name> : <BaseClass>
{
// members
}
| Part | What it is |
|---|---|
sealed | Stops other classes from inheriting from this one |
<Name> | The class's name |
: <BaseClass> | Optional: a class this one inherits from. A sealed class can still have a base class |
The reference shows the second form with two classes, class A and sealed class B : A: class B inherits from class A, but no class can inherit from class B.
The Example
A console app called SealedClass has an ordinary Vehicle class and a sealed Bicycle class that inherits from it. The program creates a bicycle, then asks .NET which of the two classes is sealed.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n SealedClass
cd SealedClass
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/SealedClass/SealedClass.csproj:
Determining projects to restore...
Restored /Users/you/source/SealedClass/SealedClass.csproj (in 60 ms).
Restore succeeded.
dotnet new console -n SealedClass creates a folder named SealedClass with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or SealedClass.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First the code that runs:
Bicycle bike = new() { Name = "City bike", Gears = 7 };
Console.WriteLine($"{bike.Name} with {bike.Gears} gears");
// IsSealed tells whether a class is declared sealed.
Console.WriteLine($"Vehicle is sealed: {typeof(Vehicle).IsSealed}");
Console.WriteLine($"Bicycle is sealed: {typeof(Bicycle).IsSealed}");
new() { Name = "City bike", Gears = 7 } creates a bicycle and sets two properties: Name comes from Vehicle and Gears from Bicycle. typeof(Vehicle) gives the class's Type, and its IsSealed property is, in the .NET reference's words, true if the Type is declared sealed; otherwise, false.
Then the two classes, at the end of the file:
// An ordinary class: other classes can inherit from it.
class Vehicle
{
public string Name { get; set; } = "";
}
// sealed: Bicycle inherits from Vehicle, but no class can inherit from Bicycle.
sealed class Bicycle : Vehicle
{
public int Gears { get; set; }
}
sealed goes in front of class. : Vehicle still makes Bicycle inherit Name from Vehicle: sealing stops classes from inheriting from Bicycle, not Bicycle from inheriting.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
City bike with 7 gears
Vehicle is sealed: False
Bicycle is sealed: True

The bicycle works like any other object: its Name, inherited from Vehicle, printed City bike, and its Gears printed 7. IsSealed is False for Vehicle, which other classes can still inherit from, and True for Bicycle.
The Complete File

Bicycle bike = new() { Name = "City bike", Gears = 7 };
Console.WriteLine($"{bike.Name} with {bike.Gears} gears");
// IsSealed tells whether a class is declared sealed.
Console.WriteLine($"Vehicle is sealed: {typeof(Vehicle).IsSealed}");
Console.WriteLine($"Bicycle is sealed: {typeof(Bicycle).IsSealed}");
// An ordinary class: other classes can inherit from it.
class Vehicle
{
public string Name { get; set; } = "";
}
// sealed: Bicycle inherits from Vehicle, but no class can inherit from Bicycle.
sealed class Bicycle : Vehicle
{
public int Gears { get; set; }
}
Try to Inherit from the Sealed Class
To see what sealed blocks, add a class that derives from Bicycle at the end of Program.cs:
class ElectricBicycle : Bicycle
{
}
Run dotnet run again. The build fails before anything runs:
error CS0509: 'ElectricBicycle': cannot derive from sealed type 'Bicycle'
Delete ElectricBicycle and the program builds and runs as before.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
abstract and sealed on the same class | error CS0418: 'Shape': an abstract type cannot be sealed or static | Choose one. The reference: It's an error to use the abstract modifier with a sealed class, because an abstract class must be inherited by a class that provides an implementation of the abstract methods or properties. |
sealed on a method that doesn't override anything | error CS0238: 'Lamp.TurnOn()' cannot be sealed because it is not an override | Leave it off. Microsoft's reference: By default, methods are non-virtual. You can't override a non-virtual method. Use sealed on a method only together with override |
A protected member in a sealed class | The build warns: warning CS0628: 'Bicycle.Gears': new protected member declared in sealed type | Make it private or public: no class can derive from a sealed class, so protected gives access to nobody extra |
FAQ
Can I seal just one method?
Yes, a method that overrides a virtual one: public sealed override string Play(). The reference: By using this approach, you enable developers to derive classes from your class while preventing them from overriding specific virtual methods or properties. A class that tried to override it anyway didn't build: error CS0239: 'LoudAlarm.Play()': cannot override inherited member 'Alarm.Play()' because it is sealed.
When should I seal a class?
The reference suggests weighing two points: The potential benefits that deriving classes might gain through the ability to customize your class. and The potential that deriving classes could modify your classes in such a way that they no longer work correctly or as expected. Microsoft's programming guide adds: Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster.
Why can't I inherit from string?
Because .NET seals it: typeof(string).IsSealed printed True. Structs are sealed too: Because structs are implicitly sealed, you can't inherit from them.
Conclusion
Write sealed in front of class when no other class should inherit from yours. The class works as before and can still inherit from a base class, but a class that tries to derive from it stops the build with CS0509.
Additional Resources
- sealed (C# reference) — the modifier on classes, methods and properties.
- Abstract and sealed classes and class members — sealed classes next to abstract ones.
- Type.IsSealed — checking whether a class is sealed.
