Static And Sealed Class In C#

In this article you will learn about static and sealed lass 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:

  1. public static class MathUtility  
  2. {  
  3.     public static int Add(int a, int b)  
  4.     {  
  5.         returna + b;  
  6.     }  
  7. }  
We can call it in the following way:
  1. 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.
  1. double dub = -3.14;  
  2. Console.WriteLine(Math.Abs(dub));  
  3. Console.WriteLine(Math.Floor(dub));  
  4. Console.WriteLine(Math.Round(Math.Abs(dub)));  
Output:

3.14
-4
3


As 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:

  1. classBaseClass  
  2. {  
  3.   
  4. }   
  5. sealed class SealedClass : BaseClass  
  6. {  
  7.   
  8. }  
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.
  1. class A  
  2. {  
  3.     protected virtual void Foo()  
  4.     {  
  5.         Console.WriteLine("A.Foo");  
  6.     }  
  7.     protected virtual void Bar()  
  8.     {  
  9.         Console.WriteLine("A.Bar");  
  10.     }  
  11. }  
  12. class B: A  
  13. {  
  14.     sealed protected override void Foo()  
  15.     {  
  16.         Console.WriteLine("B.Foo");  
  17.     }  
  18.     protected override void Bar()  
  19.     {  
  20.         Console.WriteLine("B.Bar");  
  21.     }  
  22. }  
Now see this:
  1. class C: B  
  2. {  
  3.     // Attempting to override Foo causes compiler error CS0239.   
  4.     // protected override void Foo() { Console.WriteLine("C.Foo"); }  
  5.     // Overriding F2 is allowed.   
  6.     protected override void Bar()  
  7.     {  
  8.         Console.WriteLine("C.Bar");  
  9.     }  
  10. }  
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.