0
0
CsharpConceptBeginner · 3 min read

Sealed Method in C#: What It Is and How to Use It

A sealed method in C# is a method that cannot be overridden by any class that inherits from the class where it is declared. It is used to stop further changes to a method's behavior in derived classes, ensuring the method's implementation remains fixed.
⚙️

How It Works

Imagine you have a recipe that you want to share with your friends, but you want to make sure that no one changes the final step. In C#, marking a method as sealed is like locking that final step so no one can alter it further.

When you create a class that inherits from another, you can usually change (override) how some methods work. But if the original method is marked as sealed, it means the method is locked at that level and cannot be changed by any classes that come after.

This is useful when you want to allow some changes in behavior but keep certain parts fixed to avoid mistakes or unexpected results.

💻

Example

This example shows a base class with a virtual method, a derived class that overrides and seals the method, and a further derived class that cannot override it anymore.

csharp
using System;

class BaseClass
{
    public virtual void ShowMessage()
    {
        Console.WriteLine("BaseClass message");
    }
}

class DerivedClass : BaseClass
{
    public sealed override void ShowMessage()
    {
        Console.WriteLine("DerivedClass sealed message");
    }
}

class FurtherDerivedClass : DerivedClass
{
    // This will cause a compile error if uncommented:
    // public override void ShowMessage()
    // {
    //     Console.WriteLine("FurtherDerivedClass message");
    // }
}

class Program
{
    static void Main()
    {
        BaseClass baseObj = new BaseClass();
        baseObj.ShowMessage();

        DerivedClass derivedObj = new DerivedClass();
        derivedObj.ShowMessage();

        FurtherDerivedClass furtherObj = new FurtherDerivedClass();
        furtherObj.ShowMessage();
    }
}
Output
BaseClass message DerivedClass sealed message DerivedClass sealed message
🎯

When to Use

Use a sealed method when you want to allow a method to be overridden once but prevent any further changes in subclasses. This helps keep important behavior consistent and avoids bugs caused by unexpected overrides.

For example, in a security system, you might allow customization of some methods but seal critical ones to ensure they always enforce security rules.

It is also useful in large projects where many developers work on different parts, helping maintain control over key method behaviors.

Key Points

  • A sealed method cannot be overridden by any class that inherits from the class where it is sealed.
  • It is used together with override to stop further overriding.
  • Helps maintain consistent behavior and avoid accidental changes.
  • Only methods marked virtual or override can be sealed.

Key Takeaways

A sealed method prevents further overriding in derived classes to keep behavior fixed.
You seal a method by using the sealed keyword with override in a derived class.
Sealed methods help avoid bugs from unexpected method changes in inheritance.
Only virtual or override methods can be sealed to stop further overrides.