Hi Friends,
What is Sealed Class? and in what scenario we go for Sealed Class.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted May 6, 2013, 9:52 AM EDT
There are no hard and fast rules on this but the writer of the class would make it sealed if (s)he felt that it would not be appropriate for it to act as a base class.
Perhaps the best known example of a sealed class is the System.String class in the .NET framework.
Ajitender VijayRank #6,843 · 255 reputationPosted May 6, 2013, 10:28 AM EDT
we can do same with private class also then why we go for sealed Class
VulpesRank #55 · 29.2K reputationPosted May 6, 2013, 10:47 AM EDT
using System;
class A
{
private class B
{
}
private class C : B
{
}
}
class D
{
static void Main()
{
}
}
So, it's a different thing altogether to a 'normal' sealed class which is usually a public or internal 'top level' class though it doesn't have to be.
In the first version of C#, classes which contained nothing but static members were usually sealed and contained private constructors to prevent instantiation.
However, with the advent of static classes in C# 2.0 which are automatically sealed and cannot be inherited from, it's no longer necessary to 'roll your own'.
Jignesh TrivediRank #16 · 50.3K reputationPosted May 6, 2013, 7:44 PM EDT
hi,
A sealed class cannot be inherited by other class. we can used sealed calss to prevent inheritance of the class.
The design intent of a sealed class is to indicate that the class is specialized and there is no need to extend it to provide any additional functionality.
A sealed class is mostly used for security reasons by preventing unintended derivation by which the derived class may corrupt the implementation provided in the sealed class.
hope this will help you.
Suma M161 reputationPosted May 7, 2013, 9:47 PM EDT
Please give some existing examples of sealed class in .net assembly
VulpesRank #55 · 29.2K reputationPosted May 8, 2013, 7:21 AM EDT
Jignesh TrivediRank #16 · 50.3K reputationPosted May 8, 2013, 7:50 PM EDT
ObsoleteAttribute
System.Data.SqlClient.SqlCommand
System.Data.SqlClient.SqlConnection
System.Data.SqlClient.SqlDataAdapter
System.Data.SqlClient.SqlError
Suma M161 reputationPosted May 10, 2013, 8:01 AM EDT
VulpesRank #55 · 29.2K reputationPosted May 10, 2013, 8:58 AM EDT
Harshal Shah44 reputationPosted May 11, 2013, 8:49 PM EDT
Jignesh TrivediRank #16 · 50.3K reputationPosted May 12, 2013, 7:27 PM EDT
agree with above answer.
you can create instance of sealed class.
Raj KumarRank #25 · 44.2K reputationPosted May 12, 2013, 7:48 PM EDT
In C#, the sealed modifier is used to define a class as sealed. In Visual Basic .NET,NotInheritable keyword serves the purpose of sealed. If a class is derived from a sealed class, compiler throws an error.
// Sealed class
sealed class SealedClass
{
In the following code, I create a sealed class SealedClass and use it from Class1. If you run this code, it will work fine. But if you try to derive a class from sealed class, you will get an error.
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;
}
}