Codectionary / Developer documentation / C#

Control Extension with sealed

Close a class hierarchy or one virtual override without claiming object immutability.

Read the syntax

Close a class hierarchy or one virtual override without claiming object immutability.

sealed prevents a further override, but FinalLabel can still inherit the class.

What it is and why to use it

A type author may support inheritance while protecting one operation from further overriding. sealed on a class prevents inheritance altogether; sealed override closes an inherited virtual member at that point in the hierarchy. These restrictions describe extension boundaries, not whether an instance can change its internal state.

How it works

A sealed override must override an eligible base member. Descendants can still inherit the implementation, and other overridable members can remain open. A sealed class can implement interfaces and contain mutable properties. Its callers still rely on ordinary method and validation contracts.

Common mistakes and limitations

Do not put sealed on an ordinary non-override method as though it makes it more secure. Do not equate sealed with readonly or thread safety. Closing inheritance can be helpful, but composition and interfaces are alternatives when consumers need controlled variation.

Run the examples locally

These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.

Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.

C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.

Create and enter a project

dotnet new console -n CSharpExample --framework net10.0
cd CSharpExample

CSharpExample.csproj settings used for these examples

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <LangVersion>12.0</LangVersion>
    <Nullable>enable</Nullable>
    <ImplicitUsings>disable</ImplicitUsings>
  </PropertyGroup>
</Project>

Build and run after saving one example in Program.cs

dotnet build
dotnet run --no-build

Continue learning

Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.

Syntax

public sealed override string Label() => "Fixed";

Examples

Close one override in an open hierarchy

Allow another derived class to inherit a fixed operation.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

Base item = new FinalLabel();
Console.WriteLine(item.Label());
class Base { public virtual string Label() => "Base"; }
class FixedLabel : Base { public sealed override string Label() => "Fixed"; }
class FinalLabel : FixedLabel { }
  1. Base declares a virtual operation and FixedLabel overrides it.
  2. sealed prevents a further override, but FinalLabel can still inherit the class.
  3. Dispatch through the Base reference reaches FixedLabel’s implementation. A new override in FinalLabel would be rejected by the compiler.
  4. Expected output: Fixed
  5. Declare supported extension points deliberately.

See that sealed does not freeze state

Mutate an instance through its public API.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

var counter = new Counter();
counter.Increment();
Console.WriteLine(counter.Value);
sealed class Counter {
    public int Value { get; private set; }
    public void Increment() => Value++;
}
  1. Counter cannot be subclassed.
  2. Its Increment method still updates its private-set property.
  3. The output demonstrates that inheritance restrictions and immutability are independent design decisions.
  4. Expected output: 1
  5. Declare supported extension points deliberately.

Best practices

  • Declare supported extension points deliberately.
  • Distinguish inheritance restrictions from state and security guarantees.

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading