Differences between the Sealed class and Static class in C#In this article, we will discuss the difference between the Sealed and Static class in C#. But before discussing the differences, we must know about the Sealed and Static class in C# with their syntax and examples. What is the Sealed class in C#?The sealed class in C# is a class, which avoids inheritance. If there are two classes that are parent class and child class and the parent class is marked as the Sealed, the child class can not inherit the properties and methods of the parent in the child class. It means a sealed class cannot be used as the parent or base class. Syntax of the Sealed Class:It has the following syntax: Whenever a class is sealed, it cannot be extended because it does not allow inheritance. It means that this class cannot be changed in the future. This sealed class is used when the programmer wants to finalize the class. By seeing the sealed class, the compiler will avoid the overhead of runtime checks for calling other methods, etc. It makes the class execute faster. Example:Let us take a program to illustrate the Sealed Class in C#. Output: Explanation: The above program has two classes, the Vehicle and the Program class. Here, the Vehicle is sealed, meaning the properties of this class cannot be inherited by other classes. In the Vehicle class, there is a method named Drive, which will print some statements. There is a commented code where another class, Car, is trying to inherit the class Vehicle. It throws the compilation error, saying the sealed classes cannot be inherited after commenting on that part of the code. We can access the properties in the Program class by creating the object for the Vehicle class. What is the Static class in C#?The static class in C# is a class that cannot be instantiated. In other words, a programmer cannot create an object or an instance of the static class. It is the same as normal classes, but one main thing is that we cannot create objects of this static class. This static class contains the static members and member functions, which means the variables or fields, events, nested types, and methods are all static. These classes are mainly used to define utility functions that do not need any instance to access those static methods. They cannot be inherited. Static classes can give better runtime optimization and reduce memory overhead because new objects are not created. The syntax of the static class:It has the following syntax: Example:Let us take a program to illustrate the static class in C#. Output: Explanation: The above program has two classes: the MathHelper class, which is static, and the Main class. The MathHelper class is static, meaning we cannot create objects for that class. This class contains only static methods. In MathHelper, there are two classes: Add and Subtract methods. In the main class, the Add method is accessed by using the static class names and stored in the variable. The Subtract method is also accessed by using its class name. After that, both the results are printed. Differences between the Sealed class and Static class in C#There are several difference between the Sealed and Static class in C#. Some main differences between the Sealed and Static class in C# are as follows: There are several difference between the Sealed and Static class in C#. Some main differences between the Sealed and Static class in C# are as follows:
Next TopicDirectory.Exists() in C# |