Static class in C# is a special class that cannot be instantiated . In other words , you cannot create an object of a static class and cannot access static members using an object. You can access the public static members (methods, fields and properties) using dot operator (ClassName.MemberName).

In .Net framework, we have certain static classes such as Console, Math, Environment and ThreadPool, these classes have only static methods and the sole purpose of these classes is to group some related members and functionality together. Lets look at below Math class .

Math Static Class OutPut 1.1

If you look at above Math class, then you can find all static methods in it, these methods can be accessed by using dot(.) operator (ClassName.MemberName) as follows.

Key Properties of Static Classes :

  • The class only have static members (fields, methods, properties and events). You cannot have any instance field or instance method in it, the C# compiler throws an error if you do so.
  • The class cannot be used to implement any interfaces because interface methods are callable only if there is any instance of the class available.

Create a Static Class in C# ?

You can create a static class by adding static modifier before the class name and after the access modifier as follows.

Static class and static method example:

In below example,

  • We have created a static class as MathsCalculator .
  • It has a static methods as static int CalculateMax(int[] arrNum) and static int CalculateMin(int[] arrNum) that takes an integer array and returns maximum and minimum value.
  • In the main method , we are invoking both static method by using dot (.) operator.

When we run above example, then below out put generated.

Static Class OutPut 1.0

Here, in above we invoked static methods static int CalculateMax(int[] arrNum) and static int CalculateMin(int[] arrNum)  method by using dot (.) operator as follows.

How C# compiler handles it ?

C# compiler marks the class as Abstract and Sealed. If you look at the below IL (Intermediate Language) code of above sample, generated through ILDASM (IL Disassembler) tool , then you can notice that the C# compiler has converted the class MathsCalculator to a Abstract and Sealed class as .class public abstract auto ansi sealed beforefieldinit StaticClassDemo.MathsCalculator .

Static Class OutPut 1.1

The purpose of having Abstract and Sealed modifier is as follows :

  • Making the class as Abstract, the C# compiler ensures that no other class can create an instance of it .
  • Making it as Sealed , the C# compiler ensures that no class can derive from it.

Important points about static class in C# :

Following are some of the key points.

  • The class must have only static members (fields, methods, properties and events).
  • C# compiler throws a compile error, if it has any other instance members in it.
  • It cannot contain instance constructors, however it can have static constructors to initialize static fields.
  • C# compiler automatics marks it as Abstract and Sealed during compilation time. Which ensures that, no one can create any instance of it nor it can be extended.

Thanks for your time 🙂 keep visit my other articles and also check out my article on Partial Class in C#