Static Class in C#

Last Updated : 17 Sep, 2025

In C#, a static class is a class declared using the static keyword. It can contain only static members such as fields, methods, properties and events. A static class cannot be instantiated or inherited and all its members are accessed through the class name.

Static classes are commonly used to group utility or helper methods that do not depend on object state.

Syntax: 

static class Class_Name
{
// static data members
// static method
}

Key Points

  • A static class is declared using the static keyword.
  • All members inside a static class must be static.
  • It cannot be instantiated using the new keyword.
  • It is implicitly sealed, so it cannot be inherited.
  • A static class can only have the public or internal access modifiers (it cannot be private, protected, etc.).

Declaring a Static Class

A static class is defined like a normal class but prefixed with the static keyword.

C#
using System;

static class Utility
{
    public static void PrintMessage(string msg)
    {
        Console.WriteLine(msg);
    }
}

class Program
{
    static void Main()
    {
        Utility.PrintMessage("Hello World");
    }
}

Output
Hello World

Explanation:

  • Utility is static, so no objects can be created.
  • Its method is called directly with the class name.

Static Fields and Properties in a Static Class

Static classes can hold static fields and properties that maintain shared data for the application.

C#
using System;

static class Config
{
    public static string AppName { get; set; } = "MyApp";
    public static int Version { get; set; } = 1;
}

class Program
{
    static void Main()
    {
        Console.WriteLine(Config.AppName);
        Console.WriteLine("Version: " + Config.Version);
    }
}

Output
MyApp
Version: 1
  • AppName and Version are shared across the program.
  • They are accessed without creating objects.

Static Constructor in a Static Class

A static constructor initializes static data in a static class. It is called only once, automatically, before the class is used.

C#
using System;

static class Logger
{
    public static string FilePath;

    static Logger() // static constructor
    {
        FilePath = "C:\\Logs\\app.log";
        Console.WriteLine("Logger initialized");
    }
}

class Program
{
    static void Main()
    {
        Console.WriteLine("Log Path: " + Logger.FilePath);
    }
}
  • The static constructor initializes FilePath.
  • It runs once before Logger is accessed.

Restrictions of Static Classes

  • Cannot be instantiated with new.
  • Cannot contain instance members.
  • Cannot inherit or be inherited.
  • Cannot implement interfaces.

Use Cases of Static Classes

  • Utility Methods: Provide helper functionality (e.g., Math.Sqrt).
  • Configuration: Store global application settings.
  • Logging: Maintain centralized logging functionality.

Difference between static and non-static class 

Static ClassNon-Static Class
Static class is defined using static keyword.Non-Static class is not defined by using static keyword.
In static class, you are not allowed to create objects.In non-static class, you are allowed to create objects using new keyword.
The data members of static class can be directly accessed by its class name.The data members of non-static class is not directly accessed by its class name.
Static class always contains static members.Non-static class may contain both static and non-static methods.
Static class does not contain an instance constructor.Non-static class contains an instance constructor.
Static class cannot inherit from another class.Non-static class can be inherited from another class.
Comment

Explore