C# Static Class

In C#, a Static Class is a special type of class declared using the static keyword. Unlike regular classes, a static class cannot be instantiated (you can’t create an object of it) and cannot be inherited by other classes. Moreover, all the members inside a static class — such as methods, fields, and properties — must also be declared as static.

Static classes are commonly used when you want to group utility or helper methods that don’t require object creation. They are ideal for tasks like performing mathematical calculations, logging activities, or managing application configuration — basically, anywhere you need global access to certain functions without maintaining any state.

Syntax:

public static class MathHelper
{
    public static int Add(int x, int y)
    {
        return x + y;
    }
}

Usage:

public class Program
{
    public void Main(string[] args)
    {
        int result = MathHelper.Add(10, 5);
        Console.WriteLine(result); // Output: 15
    }
}

Key Characteristics of Static Classes in C#:
  1. Cannot be instantiated — You can’t use new to create an object of it
  2. All members are static — Every method or variable inside static class must be static.
  3. Cannot inherit or be inherited — Static classes are implicitly sealed.
  4. Commonly used in utility, helper, or constants classes.

Limitations of Static Classes in C#:
While static classes are extremely useful for defining global utilities or helper methods, they do come with certain restrictions that developers should be aware of:
  • Cannot Implement Interfaces: Static classes in C# do not support interface implementation, which means you can’t use them for polymorphic behavior or dependency injection.
  • Cannot Be Inherited or Extended: Since static classes are sealed by default, they cannot be inherited or extended. This limits flexibility when you want to modify or enhance existing functionality.
  • Challenging to Unit Test: Testing static classes can be difficult, especially with mocking frameworks, because static members are tightly bound to the class and can’t be easily replaced or overridden during tests.
Real-World Example: Utility Logger

public static class Logger
{
    public static void LogInfo(string message)
    {
        Console.WriteLine($"[INFO]: {message}");
    }

    public static void LogError(string message)
    {
        Console.WriteLine($"[ERROR]: {message}");
    }
}

class Program
{
    static void Main()
    {
        Logger.LogInfo("Application started");
        Logger.LogError("Something went wrong");
    }
}

Output:

[INFO]: Application started
[ERROR]: Something went wrong


Top C# Static Class Interview Questions and Answers

If you’re preparing for a C# or .NET developer interview, understanding static classes is crucial. Below are the most common C# static class interview questions with clear and concise answers.

  1. What is a static class in C#?
    A static class in C# is a special type of class that cannot be instantiated or inherited. It is designed to contain only static members such as methods, properties, and fields that belong to the class itself rather than to an object.

  2. Can we create an object of a static class?
    No, you cannot create an object of a static class using the new keyword. Static classes are accessed directly using the class name.

  3. Can a static class have instance members?
    No. All members inside a static class must be static. You cannot define instance members or constructors in a static class.

  4. Can a static class inherit another class?
    No, static classes cannot inherit or be inherited. They are implicitly sealed, meaning inheritance and extension are not allowed.

  5. Can a static class implement an interface?
    No, static classes cannot implement interfaces, as they do not support object-oriented polymorphism or instance behavior.

  6. Can we create a constructor inside a static class?
    Yes, a static class can contain a static constructor. However, it cannot take parameters and is automatically called by the CLR only once, before the class is used for the first time.

  7. When is a static constructor called?
    A static constructor is automatically called by the Common Language Runtime (CLR) before the class is accessed for the first time — typically when a static member is referenced.

  8. What is the difference between a static class and a singleton?

    Feature Static Class Singleton
    Instantiation Cannot be instantiated Single instance is created
    Members All are static Can have both static and instance members
    Inheritance Not allowed Can implement interfaces and inherit classes
    Usage Utility or helper methods Shared instance across the app

  9. Can static classes be nested inside non-static classes?
    Yes, a static class can be declared inside another class as a nested class. This is often done to logically group utility methods related to the outer class.

  10. What are some examples of static classes in the .NET Framework?
    Here are a few commonly used static classes in .NET:

    • Math — Provides mathematical functions like Math.Sqrt() or Math.Round().
    • Console — Used for input/output operations in console applications.
    • File — Offers methods for file manipulation.
    • Environment — Provides information about the system and environment variables.
    • Path — Helps in handling and manipulating file paths.