What is a static class in C#?

A static class in C# is a class that cannot be instantiated and can only contain static members. Static classes are implicitly sealed, meaning they cannot be inherited, and they cannot contain instance constructors or non-static members.

Static classes are useful for grouping related utility methods and constants that don't require object instantiation. They are loaded automatically by the .NET runtime when first referenced.

Syntax

Following is the syntax for declaring a static class −

public static class ClassName {
    public static ReturnType MethodName() {
        // static method implementation
    }
    
    public static DataType FieldName = value;
}

Key Characteristics of Static Classes

  • Cannot be instantiated − You cannot create objects using the new keyword.

  • All members must be static − Instance members are not allowed.

  • Implicitly sealed − Cannot be inherited or serve as a base class.

  • No instance constructors − Only static constructors are permitted.

Static vs Non-Static Class Static Class ? Cannot instantiate ? Only static members ? Implicitly sealed ? No instance constructor ClassName.Method() Non-Static Class ? Can instantiate ? Instance members ? Can be inherited ? Instance constructor obj.Method()

Example

using System;

public static class Demo {
    public static float PI = 3.14f;
    public static int calc(int n) { return n * n; }
}

class Program {
    public static void Main(string[] args) {
        Console.WriteLine("PI: " + Demo.PI);
        Console.WriteLine("Square: " + Demo.calc(3));
    }
}

The output of the above code is −

PI: 3.14
Square: 9

Using Static Constructor

A static class can have a static constructor to initialize static fields. The static constructor is called automatically before any static member is accessed −

using System;

public static class MathUtility {
    public static double PI;
    public static double E;
    
    static MathUtility() {
        PI = 3.14159265359;
        E = 2.71828182846;
        Console.WriteLine("Static constructor called");
    }
    
    public static double CircleArea(double radius) {
        return PI * radius * radius;
    }
}

class Program {
    public static void Main(string[] args) {
        Console.WriteLine("Circle Area: " + MathUtility.CircleArea(5));
        Console.WriteLine("E value: " + MathUtility.E);
    }
}

The output of the above code is −

Static constructor called
Circle Area: 78.5398163395
E value: 2.71828182846

Common Use Cases

Use Case Example
Utility Methods Math operations, string formatting, validation
Constants Application-wide constants, configuration values
Extension Methods Adding functionality to existing types
Factory Methods Creating objects without instantiating the factory

Conclusion

Static classes in C# provide a way to group related functionality without requiring object instantiation. They are ideal for utility methods, constants, and extension methods where object state is not needed. Remember that static classes are sealed and can only contain static members.

Updated on: 2026-03-17T07:04:35+05:30

617 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements