Sealed: Preventing Inheritance in C#
Tags: csharp, dotnet, sealed, programming
Published: 2024-01-07 22:39:38

> By the end of this article, you can expect to:
> 
> - **Remember and Understand:** Get a clear idea of what the "sealed" keyword is and what it does in C#.
> 
> - **Know Where and How to Use It:** Learn the practical aspects of when and how to use this "sealed" keyword in your C# code.


In the world of object-oriented programming, inheritance is a powerful technique that allows you to create new classes based on existing ones. This can be incredibly useful for code reuse and modularity, but it also introduces the potential for unexpected behavior if not handled carefully. To address this, C# provides the `sealed` keyword, which serves as a protective barrier, preventing inheritance from certain classes or class members.

**Defining "Sealed"**

The "sealed" keyword is a modifier that restricts the ability of other classes to inherit from a specified class. Once a class is declared as sealed, it becomes a final class, unable to be extended any further. This ensures that the class's structure and behavior remain intact, preventing potential inconsistencies or unexpected changes.

**Why Use "Sealed"**

There are several reasons why you might want to use sealed classes:

* **Encapsulation and Code Protection:** Sealing a class can help to protect its internal implementation and prevent unauthorized modifications. This can be particularly important for classes that handle sensitive data or perform critical operations.

* **Security Considerations:** Sealing classes can be a valuable tool in security-critical applications. By preventing inheritance, you can reduce the risk of unexpected behavior or malicious code injection.

* **Controlling Class Structure:** Sealing can be used to define a class hierarchy that is more rigid and predictable. This can be useful for ensuring that classes are used in the intended manner and that their behavior is consistent.

**When to Use Sealed Classes**

Sealed classes are typically used when you want to create a class that should not be modified or extended by other classes. This could include:

* **Utility Classes:** Classes that provide essential functionality or utilities are often sealed to prevent their behavior from being inadvertently altered.

* **Base Classes with Fixed Behavior:** Classes that define a core set of functionality and should not be customized are often sealed.

* **Performance-Critical Classes:** Sealing can improve performance by reducing the overhead of dynamic dispatch, especially in cases where a class is frequently instantiated or used.

> The performance benefits of sealed classes are often minor and depend on specific code patterns and compiler decisions. (See [this link](https://josipmisko.com/posts/c-sharp-sealed-class) for more information)

* **Core System Components:** Classes that form the foundation of a software application, such as fundamental data types or operating system interfaces, should be sealed to prevent unexpected or incompatible behavior from derived classes.

* **Custom Data Structures:** Custom data structures, such as linked lists or trees, should be sealed to prevent their underlying implementation from being altered or misused by derived classes.

**Example of 'Sealed' Class**

Consider a class representing a `Shape` with properties like `color` and `area`. This abstract class can have sealed derived classes for specific shapes like `Circle`, `Rectangle`, and `Triangle`. Each derived class can provide its unique implementation of calculating the area.

```c#
abstract class Shape {
    public abstract double CalculateArea();
}

sealed class Circle(double radius) : Shape {
    public override double CalculateArea() {
        return Math.PI * radius * radius;
    }
}

sealed class Rectangle(double width, double height) : Shape {
    public override double CalculateArea() {
        return width * height;
    }
}
```
> Primary Constructor is used in this code. Are you not familiar? Read [this article](https://dev.to/hamidmolareza/primary-constructor-in-c-12-streamlining-object-creation-9lh).

**Conclusion**

The "sealed" keyword is a valuable tool for controlling class inheritance and ensuring code integrity in C#. By understanding its purpose and using it judiciously, you can create more robust and maintainable object-oriented applications.

> AI has also been used to collect and rewrite this content.