Sealed means to preventing from someone or keeping something safe, So here in C# Sealed means to preventing other Classes to inherit the Sealed Class. Using Sealed Modifier we can declare any Class as a Sealed Class and this Sealed Class act like a guard for that Class and member of the Class to prevent unauthorised access or inherit them.
Sealed Classes are widely used in Singleton Design Patterns and if you want to prevent the actual functionality of the Class to be accessed by other Classes then make that Class Sealed so that you can get Stability and Performance.
When the Class has Sealed keyword then that class is act as a Sealed Class as shown in below code , I have created a SuperAdmin class with Sealed Keyword and inside this SuperAdmin Class , one Static Constructor and one non static constrauctor with one int parameter is defined. As you can observe I used Public modifier with instance Constractor and here we have defined few static and non static members inside the Sealed Class.
internal sealed class SuperAdmin
{
static SuperAdmin()
{
}
public SuperAdmin(int a)
{
}
public static int rollnumber;
private bool disposedValue;
public string name { get; set; } = string.Empty;
public void BusRouteDetails(string busNumber)
{
Console.WriteLine($"the route for bus {busNumber} is street mall to railway station");
}
public void PrintClassName()
{
Console.WriteLine("This is Super Admin Sealed Class");
}
}
Note :- Sealed Class can have Static and Non-Static(instance) members and Constructors. As you can see in above code snippet , rollnumber is a Static Member and name is a instance member and also we have created two constrcutors one is Static and other one is instance.