Friday, May 12, 2023

Sealed class in C#

What is sealed class in C#?


 In C#, a sealed class is a class that cannot be inherited by other classes. Once a class is declared as sealed, it cannot be extended or derived from by any other class. This means that you cannot create a new class that inherits from a sealed class.


Here's an example of a sealed class:


sealed class clsEmployee

{

    public string Name { get; set; }

    public int Age { get; set; }

    public decimal Salary { get; set; }

}



In this example, the "clsEmployee" class is declared as sealed, which means that it cannot be inherited by any other class. The class has properties for the name, age, and salary of an employee.


Sealed classes are useful in scenarios where you want to restrict the inheritance of a class. By sealing a class, you can prevent other developers from extending or modifying the behavior of the class. This can be particularly useful when you are creating a class that has sensitive or critical functionality, where changes to the behavior of the class could have serious consequences.


It's important to note that you can only seal a class that is not already marked as abstract. Additionally, you cannot mark individual members of a class as sealed – only the entire class can be sealed.

No comments:

Post a Comment

If you have any query kindly let me know.

Blazor drawback| drawback of blazor| Disadvantage of blazor in c#

  Blazor drawback| drawback of blazor| Disadvantage of blazor in c# While Blazor offers many advantages, it also has a few drawbacks to cons...