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.

Naimish MakwanaPosted Jan 19, 2023, 11:00 AM
Nice article. Thanks for sharing.
Dinesh KumarPosted Dec 7, 2021, 10:18 AM
Thank you very much sir. Its very helpful explanation.
Vinodh ShanmugasundaramPosted May 12, 2021, 9:13 AM
Very use full explanation thank you sir...
Vinodh ShanmugasundaramPosted May 12, 2021, 9:10 AM
Very useful... Thank you
Amit Kumar SinghPosted Mar 4, 2021, 3:31 AM
Nice one .. Thank you !!
Sagar ChowdhuryPosted Jun 28, 2020, 5:13 PM
Nice explanation .
Farzin TaratPosted Apr 27, 2020, 10:22 AM
Great article
Yogesh KumarPosted Dec 14, 2019, 9:29 PM
Thank you very much
Bidyasagar MishraPosted Aug 3, 2019, 9:10 PM
Great article sir.
ajay ruhelaPosted Jul 31, 2019, 9:14 AM
Nice article, but I have a question if The main purpose of a sealed class is to take away the inheritance feature from the class users so they cannot derive a class from it , then why do we are allowed to write the extension methods that behaves like a member from the sealed class.?
Adedotun SalamiPosted Jun 20, 2019, 3:09 AM
Its a nice one
Shamim UddinPosted Feb 17, 2019, 7:13 PM
Thank you very much sir. Its very helpful explanation.
Puneet KankarPosted Feb 17, 2019, 11:20 AM
Very useful article for everyone thanks, sir..!!
Sachin SuryawanshiPosted Oct 26, 2018, 8:34 AM
Can we use sealed class in singleton pattern?
Avinash PhadkePosted Oct 26, 2018, 1:38 AM
Really Good Article !!!
Rushi MehtaPosted Oct 25, 2018, 6:01 AM
Good Article
SudhanshuPosted Apr 27, 2018, 9:34 PM
Good Article :) How can we contribute more on this forum.
Amit Kumar SinghPosted Mar 11, 2018, 12:23 PM
Nice one sir... thanks
rupali patilPosted Nov 30, 2017, 2:43 AM
Very nice. please tell me the real life example of sealed class.
somesh bhowmickPosted Nov 18, 2017, 11:33 AM
Very clear sir, Thanks for posting
Fernando FerreiraPosted Nov 6, 2017, 10:56 AM
Thank you very much. Its very helpful explanation.
Alok TripathiPosted Mar 10, 2017, 4:03 AM
Great sir, really helpful article
prathapPosted Dec 7, 2016, 9:33 AM
Perfect. Things clerified here for article http://www.c-sharpcorner.com/uploadfile/puranindia/what-are-sealed-classes-and-sealed-methods/
SubashPosted Sep 27, 2016, 2:23 AM
Thank you sir now i get clear view about sealed class but i slightly confused about the sealed modifier if you don't mind can you give little brief about sealed modifier
Thiruppathi RPosted Jun 27, 2016, 9:45 AM
Nice Article..
Mohammad KhalidPosted May 18, 2016, 2:34 AM
Going through basic concept is always good. I keep reading and enjoy .... I never studied religiously so I started now . Its never too late to start good thing.
Pawan TiwariPosted Jan 21, 2016, 7:09 AM
Helpful Article.. Thank you sir
Akshita ThakkarPosted Jan 12, 2016, 6:23 AM
Super Sir....
Sujeet SumanPosted Oct 8, 2015, 6:01 AM
Nice article sir.............
RAHSAN GOKSUPosted Jan 28, 2015, 7:25 AM
good thank you
Jeetendra GundPosted Aug 28, 2013, 9:10 AM
My query solved using this article...Thank u.
Vithal WadjePosted Aug 25, 2013, 3:20 AM
Super article
om prakashPosted Aug 24, 2013, 2:00 AM
boring
sriram vPosted Jun 5, 2013, 2:23 AM
Simple Nice . Very Useful for us.
Sachin JaineditedPosted Apr 1, 2013, 5:07 AMEdited Apr 1, 2013, 5:08 AM
Simple....
Praveen VRPosted Jan 24, 2013, 1:28 AM
Nice..Good Information for me
shanmugam RPosted Nov 21, 2012, 12:59 AM
nice... Simple and good... Thank you...
michael meyerPosted Jun 11, 2012, 7:21 AM
Nice article.. You can also visit below link for more information http://www.hyperneed.com/ShowSearchAnswers.aspx?searchstring=sealed&category=Programming&questionid=e326c0b7-3bf9-4612-8be6-7cc3012a82da
Prashant JainPosted Dec 29, 2011, 2:56 AM
How is it going to be advantage while making a class as sealed if it has all the members as static Could you please throw some light on this.
jhnPosted Apr 25, 2011, 4:47 AM
need more expl dude !!!
Govindaraj KrishnanPosted Apr 18, 2011, 5:47 AM
This is wonderfull exampled for sealed class
renuPosted Oct 14, 2010, 4:09 PM
Can a sealed class have a virtual method
Benjamin BabaduliPosted Feb 1, 2010, 5:01 PM
Great job! However I may disagree with the argumentation of using sealed classes to host static only content. There is a static kyoword on a class for this purposes. This post contains some good motivations on this issue http://geekswithblogs.net/gmamaladze/Default.aspx
chigicherla MallikarjunaPosted Feb 17, 2009, 7:18 AM
pleaseilluistrate with good example.
Vinayaka VernekarPosted Dec 2, 2008, 7:55 AM
What is the difference between Sealed and Static class? When to use Sealed and static class?
shefali kapadiaPosted Sep 30, 2008, 12:49 AM
what is proxy class and why we need to create it in WCF ???
arvind kumar 0Posted Mar 17, 2008, 5:16 AM
what is the differecnce between c# and java
a sPosted Sep 23, 2007, 3:11 PM
Why would .NET framework designers take the trouble of providing a sealed keyword? What difference would it make even if the Pens class was not sealed and somebody derives it?
prakash rajaPosted Jan 31, 2007, 9:32 AM
whay is the difference b/w interface and abstractclass..? some body pls..give me right answer... regrards.. Prakash