Differences Between The Sealed Class And Static Class In C#

In this guide, we will explore the variances between Sealed and Static classes in C#. Prior to delving into the disparities, it's essential to comprehend the concepts of Sealed and Static classes in C# along with their syntax and instances.

What is the Sealed class in C#?

The sealed class in C# is a type of class that prevents inheritance. When a parent class is marked as sealed, any child class cannot inherit the properties and methods of the parent class. This restriction essentially means that a sealed class cannot serve as a base or parent class for other classes.

Syntax of the Sealed Class:

It has the following syntax:

Example

sealed class ClassName {
    // Class members
}

Whenever a class is marked as sealed, it becomes unextendable, prohibiting any inheritance. This indicates that modifications to this class are restricted in the future. The sealed class is typically employed when the developer intends to firmly establish the class structure. By designating a class as sealed, the compiler can bypass unnecessary runtime validations when invoking various methods, thereby enhancing the execution speed of the class.

Example:

Let's consider a code example to demonstrate the concept of Sealed Class in the C# programming language.

Example

using System;
sealed class Vehicle
{
    // Instance member
    public void Drive()
    {
        Console.WriteLine("Vehicle is being driven.");
    }
}
// Attempting to inherit from a sealed class will result in a compilation error
// class Car : Vehicle {} // This line will cause a compilation error
class Program
{
    static void Main(string[] args)
    {
        // Creating an instance of the sealed class
        Vehicle vehicle = new Vehicle();
        vehicle.Drive(); // Output: Vehicle is being driven.
    }
}

Output:

The given code snippet demonstrates the styling for a placeholder element. It includes a CSS class named "placeholder-diagram" with specific properties like background color, border radius, padding, margin, and text alignment. Within this class, there are further styles defined for the placeholder icon and text.

Explanation:

The program provided above consists of two distinct classes: the Vehicle class and the Program class. In this scenario, the Vehicle class is marked as sealed, indicating that its characteristics cannot be extended or inherited by any other classes. Within the Vehicle class, there exists a method named Drive, responsible for outputting specific statements. Additionally, there is a section of commented-out code where an attempt is made for another class, Car, to inherit properties from the Vehicle class. However, due to the sealed nature of the Vehicle class, this results in a compilation error. Once this section is commented out, the properties within the Vehicle class can still be accessed in the Program class by instantiating an object of the Vehicle class.

What is the Static class in C#?

The static class in C# is a class that is not instantiable, meaning it cannot be used to create objects or instances. While similar to regular classes, the key distinction lies in the inability to instantiate objects from a static class.

This unchanging class comprises fixed members and member functions. This implies that the variables or fields, events, nested types, and methods are all constant. These types are primarily employed to delineate utility functions that do not necessitate any instance for utilizing those fixed methods. They are not inheritable. Static classes can enhance runtime optimization and minimize memory usage by eliminating the creation of new objects.

The syntax of the static class:

It has the following syntax:

Example

static class ClassName {
    // Static members
}

Example:

Let's consider a code example to demonstrate the concept of a static class in the C# programming language.

Example

using System;
static class MathHelper
{
    // Static method for adding two integers
    public static int Add(int a, int b)
    {
        return a + b;
    }
    // Static method for subtracting two integers
    public static int Subtract(int a, int b)
    {
        return a - b;
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Accessing static members of the static class without instance creation
        int sum = MathHelper.Add(5, 3);
        Console.WriteLine("Sum: " + sum);
        int difference = MathHelper.Subtract(10, 4);
        Console.WriteLine("Difference: " + difference);
    }
}

Output:

The <style> section defines the styling for a placeholder diagram. The CSS includes a background with a linear gradient, border radius, padding, margin, and text alignment. Within the diagram, there is a placeholder icon with specific font size and margin, as well as placeholder text with a defined color and font size. </style>

Explanation:

The program above consists of two classes: the MathHelper class, which is defined as static, and the Main class. Being static, objects cannot be instantiated for the MathHelper class, as it solely contains static methods. Within MathHelper, there are two methods: Add and Subtract. In the Main class, the Add method is called using the static class names and stored in a variable. Similarly, the Subtract method is invoked using its class name. Following this, the results from both methods are printed.

Differences between the Sealed class and Static class in C#

The <style> code snippet demonstrates the styling for a placeholder diagram. It includes CSS properties like background color, border radius, padding, margin, and text alignment. The placeholder diagram is designed with a gradient background color, rounded corners, ample padding, and centralized text alignment for a visually appealing presentation.

There are multiple distinctions between a Sealed and a Static class in C#. Some key variances between the Sealed and Static class in C# are outlined below:

There are various distinctions between a Sealed and Static class in C#. Some key variances between a Sealed and Static class in C# include:

Feature Sealed class Static class
Inheritance It cannot be inherited. Static classes are also cannot be inherited.
Instantiation We can able to create the objects for the sealed class. Object creation is not possible for the static class.
Members in class The member of the sealed class can be both static and non-static. The members of the static class, methods and properties should be static only.
Usage It is used to finalize the class. These are mainly used for utility methods which does not need objects to access.
Extension These classes cannot be further changed or extended. These cannot be inherited and its members are shared.
Performance This class will give better performance in terms of runtime. This class will give better performance in terms in runtime and memory.
Constructor Constructor can be used to initialize the variables. There is no constructor used here due to the elimination of the object creation.
Access Modifier It can have different access modifiers for members of the class. Here, all the members should be static, and the access modifier should be the same.
Memory allocation Memory is allocated for objects of the sealed class. No memory is allocated.
Polymorphism Supports polymorphism. Do not support the polymorphism.

Input Required

This code uses input(). Please provide values below: