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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Raj KumarPosted May 12, 2013, 11:48 PM
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;
}
}
Jignesh TrivediPosted May 12, 2013, 11:27 PM
agree with above answer.
you can create instance of sealed class.
Harshal ShahPosted May 12, 2013, 12:49 AM
VulpesPosted May 10, 2013, 12:58 PM
Suma MPosted May 10, 2013, 12:01 PM
Jignesh TrivediPosted May 8, 2013, 11:50 PM
ObsoleteAttribute
System.Data.SqlClient.SqlCommand
System.Data.SqlClient.SqlConnection
System.Data.SqlClient.SqlDataAdapter
System.Data.SqlClient.SqlError
VulpesPosted May 8, 2013, 11:21 AM
Suma MPosted May 8, 2013, 1:47 AM
Please give some existing examples of sealed class in .net assembly
Jignesh TrivediPosted May 6, 2013, 11:44 PM
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.
VulpesPosted May 6, 2013, 2:47 PM
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'.
Ajitender VijayPosted May 6, 2013, 2:28 PM
we can do same with private class also then why we go for sealed Class
VulpesPosted May 6, 2013, 1:52 PM
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.