Udemy

Difference between static and sealed class in c#

Saturday, June 13, 2015 0 Comments A+ a-


What is static class

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

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


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


We can call it this way:


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


A static class will be used as a convenient instrumentation for sets of ways that simply treat input parameters and don't got to get or set any internal instance fields. for instance, in the .NET Framework class Library, the static System.Math class contains functions/methods that perform mathematical operations, with none 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


As is that 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 from 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 base class by other classes.

Consider the following example:

class BaseClass 
{

}    

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 be overridden further 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 when using sealed modifier with methods/properties that method should be override.In .Net Framework structs are implicitly sealed which means they cannot be inherited.

Some run-time optimization 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
Coursera - Hundreds of Specializations and courses in business, computer science, data science, and more