Using the ‘sealed’ keyword in C#

Cengiz Akarsu
2 min readJan 24, 2024

--

The ‘sealed’ Keyword in C#: Effects on Inheritance and Performance

In C# programming language, the ‘sealed’ keyword serves as a specifier for classes and methods, influencing inheritance and method override mechanisms. When a class is marked as ‘sealed’, it prohibits other classes from inheriting from it, restricting the class from being a base class for another. This feature supports a strict hierarchy in class design that is specifically optimized for specific scenarios.

Let’s first delve into how a class is marked as ‘sealed’. When you designate a class as ‘sealed’, it signals that no other class can inherit from it. This decision fosters a strict hierarchy in class design, optimized for particular circumstances.

Example usage:

Similarly, when a method is marked as ‘sealed’, it prevents the method from being overridden by derived classes.

Example usage:

This example illustrates how ‘sealed’ classes and methods are employed. However, it is crucial to note the impact of the ‘sealed’ keyword on performance. ‘Sealed’ classes often enable the compiler to generate more optimized code, facilitating faster processing of class hierarchies at runtime. This becomes especially significant for developers seeking performance improvements in large and complex projects.

In conclusion, the ‘sealed’ keyword in the C# programming language serves as a potent tool, contributing to more organized and efficient code. Nevertheless, its usage should be carefully evaluated in each scenario, considering the trade-off between code flexibility and adaptability to future changes. As ‘sealed’ limits the flexibility of your code and adaptability to future changes, its proper usage is crucial.

--

--