Skip to content

Code Quality: Seal classes when possible #12304

Closed
@itsWindows11

Description

@itsWindows11

Description

Related to #4180.

Unsealed classes introduce extra overhead for running each virtual method/type checks because the compiler/JIT has to do extra inheritance checks.

For example, there are three non-sealed classes called ClassA, ClassB and ClassC, and ClassB inherits from ClassA. If ClassC doesn't inherit from ClassB (in other forms, if nothing inherits from ClassB) then we don't need the extra dispatch operations or checks. Thus making ClassB sealed removes that overhead when calling virtual methods or doing type checks.

See https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-6/#peanut-butter for more in-depth details.

Concerned code

Most C# source files in the entire app solution.

Gains

  • Slightly better performance.

Requirements

  • Make classes that don't need to be inherited sealed.

Comments

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Status

    ✅ Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions