Static And Sealed Class In C#

What is static class?

A static class is very similar to a non-static class, however there's one difference: a static class can’t be instantiated. In different words, you cannot use the new keyword to make a variable of that class type. As a result, there's no instance variable, you access the static class members by using class name.

For example, we have the following static class which has a static method that adds two number. This is just an example, we already have a Math class in System namespace which framework has provided that has all the commonly used Math functions available.

public static class MathUtility  
{  
    public static int Add(int a, int b)  
    {  
        returna + b;  
    }  
}  

We can call it in the following way.

int result = MathUtility.Add(2,5);  

A static class will be used as a convenient instrumentation for a set of ways that simply treat input parameters and don't get or set any internal instance fields. For example, in the .NET Framework class Library, the static System.Math class contains functions / methods that perform mathematical operations, with none of them demand to store or retrieve knowledge that's distinctive to a specific instance of the Math class. That is, you apply the members of the class by specifying the class name and also the methodology name, as shown within the following example.

double dub = -3.14;  
Console.WriteLine(Math.Abs(dub));  
Console.WriteLine(Math.Floor(dub));  
Console.WriteLine(Math.Round(Math.Abs(dub)));  

Output

3.14 -4 3

in the case with all class types, the data for a static class is loaded by the .NET Framework common language runtime (CLR) once the program that references the class is loaded. The program cannot specifically tell when the class is loaded. However, it's certain to be loaded and to own its fields initialized and its static constructor referred to as before the class is documented for the primary time in your program. A static constructor is called just once, and a static class remains in memory for the time period of the applying domain during which your program resides.

Features of Static Class

  1. It can only have static members.
  2. It cannot have instance members as static class instance cannot be created.
  3. It is a sealed class.
  4. As static class is sealed, so no class can inherit from a static class.
  5. We cannot create instance of static class that's the reason we cannot have instance members in static class, as static means shared so one copy of the class is shared to all.
  6. Static class also cannot inherit from other classes.

What is sealed class?

A sealed class cannot be inherited (means it cannot be used as a base class). It stops / restricts other classes from inheriting it. Yes, when a class is marked sealed no other classes could inherit from it.

Consider the following example in which class SealedClass inherited from class BaseClass but as we have marked SealedClass sealed using sealed modifier, it cannot be used as a base class by other classes.

Consider the following example.

classBaseClass  
{  
  
}   
sealed class SealedClass : BaseClass  
{  
  
}  

We can also make methods and properties sealed. We can also mark overridden methods or properties of base class in child class sealed so that they cannot further override sub-classes that inherit from this subclass.

class A  
{  
    protected virtual void Foo()  
    {  
        Console.WriteLine("A.Foo");  
    }  
    protected virtual void Bar()  
    {  
        Console.WriteLine("A.Bar");  
    }  
}  
class B: A  
{  
    sealed protected override void Foo()  
    {  
        Console.WriteLine("B.Foo");  
    }  
    protected override void Bar()  
    {  
        Console.WriteLine("B.Bar");  
    }  
}  

Now see this.

class C: B  
{  
    // Attempting to override Foo causes compiler error CS0239.   
    // protected override void Foo() { Console.WriteLine("C.Foo"); }  
    // Overriding F2 is allowed.   
    protected override void Bar()  
    {  
        Console.WriteLine("C.Bar");  
    }  
}  

You cannot use abstract and sealed modifier together with a class because abstract class has to be inherited by some sub-class that gives implementation of abstract methods / properties.

Also, while using sealed modifier with methods / properties that method should be overridden. In .NET Framework structs are implicitly sealed which means they cannot be inherited.

Some run-time optimizations are also done when sealed class members are called, so calling of sealed class members is slightly faster than other.

The following are the points to keep in mind about sealed class.

  1. Sealed class can be instantiated.
  2. It can inherit from other classes.
  3. It cannot be inherited.


Recommended Free Ebook
Similar Articles