Sealed Class in C#

Introduction

In this article, I will discuss creating and using a sealed class using C#. We will also see why developers use sealed classes in their libraries and code. 

Find more about Classes in C#: Types of Classes in C#

Sealed Classes in C#

Sealed classes are used to restrict the inheritance feature of object-oriented programming. Once a class is defined as a sealed class, this class cannot be inherited. 

In C#, the sealed modifier is used to declare a class as sealed. In Visual Basic .NET, the NotInheritable keyword serves the purpose of being sealed. The compiler throws an error if a class is derived from a sealed class.

If you have ever noticed, structs are sealed. You cannot derive a class from a struct. 

The following class definition defines a sealed class in C#: 

// Sealed class  
sealed class SealedClass  
{  
}   

In the following code, I create a sealed class, SealedClass, and use it from Class 1. If you run this code, it will work just fine. But you will get an error if you try to derive a class from SealedClass.

using System;  
class Class1  
{  
    static void Main(string[] args)  
    {  
        SealedClass sealedCls = new SealedClass();  
        int total = sealedCls.Add(4, 5);  
        Console.WriteLine("Total = " + total.ToString());  
    }  
}  
// Sealed class  
sealed class SealedClass  
{  
    public int Add(int x, int y)  
    {  
        return x + y;  
    }  
}   

MSDN Updated - Sealed Methods and Properties 

You can also use the sealed modifier on a method or a property that overrides a virtual method or property in a base class. This enables you to allow classes to derive from your class and prevent other developers using your classes from overriding specific virtual methods and properties.

class X  
{  
    protected virtual void F()  
    {   
        Console.WriteLine("X.F");   
    }  
    protected virtual void F2()  
    {  
        Console.WriteLine("X.F2");   
    }  
}  
class Y : X  
{  
    sealed protected override void F()  
    {  
        Console.WriteLine("Y.F");  
    }  
    protected override void F2()  
    {  
        Console.WriteLine("X.F3");  
    }  
}  
class Z : Y  
{  
    // Attempting to override F causes compiler error CS0239.  
    //   
    protected override void F()  
    {  
         Console.WriteLine("C.F");   
    }  
    // Overriding F2 is allowed.   
    protected override void F2()  
    {  
        Console.WriteLine("Z.F2");   
    }  
}  

Why Sealed Classes?

We just saw how to create and use a sealed class in C#. A sealed class's main purpose is to remove the inheritance feature from the class users so they cannot derive a class from it. One of the best uses of sealed classes is when you have a class with static members. For example, the Pens and Brushes classes of the System.Drawing namespace.

The Pens class represents the pens with standard colors. This class has only static members. For example, Pens.Blue represents a pen with blue color. Similarly, the Brushes class represents standard brushes. The Brushes.Blue represents a brush with blue color.

So when you're designing a class library and want to restrict your classes from being derived by developers, you may want to use sealed classes.


Recommended Free Ebook
Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.