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, 6:00 AM EST
Nice article. Thanks for sharing.
Dinesh KumarPosted Dec 7, 2021, 5:18 AM EST
Thank you very much sir. Its very helpful explanation.
Vinodh ShanmugasundaramPosted May 12, 2021, 5:13 AM EDT
Very use full explanation thank you sir...
Vinodh ShanmugasundaramPosted May 12, 2021, 5:10 AM EDT
Very useful... Thank you
Amit Kumar SinghPosted Mar 3, 2021, 10:31 PM EST
Nice one .. Thank you !!
Sagar ChowdhuryPosted Jun 28, 2020, 1:13 PM EDT
Nice explanation .
Farzin TaratPosted Apr 27, 2020, 6:22 AM EDT
Great article
Yogesh KumarPosted Dec 14, 2019, 4:29 PM EST
Thank you very much
Bidyasagar MishraPosted Aug 3, 2019, 5:10 PM EDT
Great article sir.
ajay ruhelaPosted Jul 31, 2019, 5:14 AM EDT
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 19, 2019, 11:09 PM EDT
Its a nice one
Shamim UddinPosted Feb 17, 2019, 2:13 PM EST
Thank you very much sir. Its very helpful explanation.
Puneet KankarPosted Feb 17, 2019, 6:20 AM EST
Very useful article for everyone thanks, sir..!!
Sachin SuryawanshiPosted Oct 26, 2018, 4:34 AM EDT
Can we use sealed class in singleton pattern?
Avinash PhadkePosted Oct 25, 2018, 9:38 PM EDT
Really Good Article !!!
Rushi MehtaPosted Oct 25, 2018, 2:01 AM EDT
Good Article
SudhanshuPosted Apr 27, 2018, 5:34 PM EDT
Good Article :) How can we contribute more on this forum.
Amit Kumar SinghPosted Mar 11, 2018, 8:23 AM EDT
Nice one sir... thanks
rupali patilPosted Nov 29, 2017, 9:43 PM EST
Very nice. please tell me the real life example of sealed class.
somesh bhowmickPosted Nov 18, 2017, 6:33 AM EST
Very clear sir, Thanks for posting
Fernando FerreiraPosted Nov 6, 2017, 5:56 AM EST
Thank you very much. Its very helpful explanation.
Alok TripathiPosted Mar 9, 2017, 11:03 PM EST
Great sir, really helpful article
prathapPosted Dec 7, 2016, 4:33 AM EST
Perfect. Things clerified here for article http://www.c-sharpcorner.com/uploadfile/puranindia/what-are-sealed-classes-and-sealed-methods/
SubashPosted Sep 26, 2016, 10:23 PM EDT
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, 5:45 AM EDT
Nice Article..
Mohammad KhalidPosted May 17, 2016, 10:34 PM EDT
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, 2:09 AM EST
Helpful Article.. Thank you sir
Akshita ThakkarPosted Jan 12, 2016, 1:23 AM EST
Super Sir....
Sujeet SumanPosted Oct 8, 2015, 2:01 AM EDT
Nice article sir.............
RAHSAN GOKSUPosted Jan 28, 2015, 2:25 AM EST
good thank you
Jeetendra GundTOP 100Posted Aug 28, 2013, 5:10 AM EDT
My query solved using this article...Thank u.
Vithal WadjeTOP 100Posted Aug 24, 2013, 11:20 PM EDT
Super article
om prakashPosted Aug 23, 2013, 10:00 PM EDT
boring
sriram vPosted Jun 4, 2013, 10:23 PM EDT
Simple Nice . Very Useful for us.
Sachin JaineditedPosted Apr 1, 2013, 1:07 AM EDTEdited Apr 1, 2013, 1:08 AM EDT
Simple....
Praveen VRPosted Jan 23, 2013, 8:28 PM EST
Nice..Good Information for me
shanmugam RPosted Nov 20, 2012, 7:59 PM EST
nice... Simple and good... Thank you...
michael meyerPosted Jun 11, 2012, 3:21 AM EDT
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 28, 2011, 9:56 PM EST
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, 12:47 AM EDT
need more expl dude !!!
Govindaraj KrishnanPosted Apr 18, 2011, 1:47 AM EDT
This is wonderfull exampled for sealed class
renuPosted Oct 14, 2010, 12:09 PM EDT
Can a sealed class have a virtual method
Benjamin BabaduliPosted Feb 1, 2010, 12:01 PM EST
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, 2:18 AM EST
pleaseilluistrate with good example.
Vinayaka VernekarPosted Dec 2, 2008, 2:55 AM EST
What is the difference between Sealed and Static class? When to use Sealed and static class?
shefali kapadiaPosted Sep 29, 2008, 8:49 PM EDT
what is proxy class and why we need to create it in WCF ???
arvind kumar 0Posted Mar 17, 2008, 1:16 AM EDT
what is the differecnce between c# and java
a sPosted Sep 23, 2007, 11:11 AM EDT
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, 4:32 AM EST
whay is the difference b/w interface and abstractclass..? some body pls..give me right answer... regrards.. Prakash