Skip to content

How to Resolve Error "CS0712: Cannot create an instance of the static class" in C#

Fix the CS0712 compiler error in C# by either accessing static class members directly (e.g., MathHelpers.Add()) or converting the class to non-static if object instances are needed.

The Compiler Error CS0712 is an instantiation error. The message reads: "Cannot create an instance of the static class 'ClassName'".

In C#, a static class is designed to be a container for global methods and data (like System.Math or System.Console). By definition, a static class cannot be instantiated. It creates no objects on the heap, and you cannot use the new keyword with it. It exists as a singular entity managed by the runtime.

This guide explains how to use static classes correctly or how to change your design if you need object instances.

Understanding Static Classes

A class marked with the static keyword is locked down:

  • It contains only static members.
  • It is implicitly sealed (cannot be inherited).
  • It acts as a library of functions rather than a blueprint for an object.

Because there is no "instance" to create, the constructor logic is handled automatically by the runtime (via a static constructor). Attempting to create one manually using new is forbidden.

Scenario: Attempting to use new

This error commonly occurs when a developer refactors a standard class into a static utility class but forgets to update the code that uses it, or when a beginner mistakes a utility class for a data entity.

Example of error:

// Defined as static
public static class MathHelpers
{
    public static int Add(int a, int b) => a + b;
}

public class Program
{
    static void Main()
    {
        // ⛔️ Error CS0712: Cannot create an instance of the static class 'MathHelpers'.
        // You cannot build an object from a static class.
        var helper = new MathHelpers(); 
        
        int result = helper.Add(1, 2); // This syntax is also invalid for static members
    }
}

Solution 1: Access Members Directly (Utility Pattern)

If the class is intended to be a collection of helper methods (like Math, Convert, or File), you do not need an instance variable. You access the members directly via the Class Name.

Solution: remove the new keyword and the variable declaration.

public static class MathHelpers
{
    public static int Add(int a, int b) => a + b;
}

public class Program
{
    static void Main()
    {
        // ✅ Correct: Access the method via the class name.
        int result = MathHelpers.Add(1, 2);
        
        System.Console.WriteLine(result);
    }
}

Solution 2: Make the Class Non-Static (Object Pattern)

If you actually need to create multiple objects, store state (data) specific to each object, or pass the object around as a parameter, then the class should not be static.

Solution: remove the static keyword from the class definition.

// ✅ Correct: Removed 'static'. Now it is a blueprint for objects.
public class Calculator
{
    // Instance member
    public int LastResult { get; private set; }

    public int Add(int a, int b) 
    {
        LastResult = a + b;
        return LastResult;
    }
}

public class Program
{
    static void Main()
    {
        // ✅ Correct: Now we can create instances.
        var calc1 = new Calculator();
        var calc2 = new Calculator(); // A completely separate object

        calc1.Add(5, 5);
        Console.WriteLine(calc1.LastResult); // 10
        Console.WriteLine(calc2.LastResult); // 0 (Distinct state)
    }
}

Conclusion

CS0712 reminds you that static classes are not objects.

  1. Check the Definition: Is the class defined as public static class?
  2. Determine Usage:
    • Utility/Helper: Don't use new. Call ClassName.Method() directly.
    • Entity/State: Remove the static keyword from the class definition so you can instantiate it.
Navigation