Sealed Class in C#

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.

Code Example of Sealed Class

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.

We can use modifiers with Sealed Classes constructors as shown in example below.

Image is not available

We can use Static and Instance Constructors in Sealed Classes as shown in example below.


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.

We can create instance of Sealed Classes as shown in example below.

Image is not available

We can not inherit Sealed Classes from other Classes as shown in example below SuperAdmin is a Sealed Class and Admin is a Class which istrying to inherit SuperAdmin class but it is showing compile time error.

Image is not available
Points to Remember
  • Point 1: Using Sealed keyword we can create a Sealed Class.
  • Point 2: Can create a Instance of Sealed Class.
  • Point 3: Can not inherit Sealed Classes by other classes.
  • Point 4: Sealed Class can implement the Interface.
  • Point 5: Can pass Sealed Class object in Method Parameters.
  • Point 6: We can use Sealed Class as a return type of methods.
  • Point 7: Sealed Classes may have Static and Non Static members.
  • Point 8: Sealed Classes can have Static and Non Static Constructors.
  • Point 9: We can use modifiers with Sealed Class Constructors..
  • Point 10: Sealed class may constructor with Parameter and Parameterless also.
  • Point 11: Sealed Classes supports the Object Disposal.
  • Point 12: We can access Sealed Class members using objects.