Skip to main content

How to Resolve Error "CS0515: Access modifiers are not allowed on static constructors" in C#

The Compiler Error CS0515 is a declaration error. The message reads: "access modifiers are not allowed on static constructors".

In C#, a Static Constructor (static ClassName() { ... }) is a special block of code used to initialize the class itself (e.g., setting values for static fields) rather than specific instances of the class. Because the Common Language Runtime (CLR) calls this constructor automatically and implicitly (usually just before the class is first used), your code never calls it directly. Since you cannot call it, access modifiers like public, private, or protected are meaningless and forbidden.

This guide explains the rules of static constructors and how to declare them correctly.

Understanding Static Constructor Visibility

A standard constructor looks like this: public MyClass() { } You mark it public so other classes can say new MyClass().

A static constructor looks like this: static MyClass() { } It has no access modifier. This is because:

  1. No one calls it: You cannot write MyClass.StaticConstructor().
  2. The CLR calls it: The runtime engine has special privileges to run this code regardless of visibility settings.

Because the concept of "Access Control" applies to other code calling your members, and no other code can call a static constructor, adding a modifier is logically invalid.

Scenario: Adding Public/Private to Static Constructors

This error is almost always a habit-based mistake. You are used to typing public before every method or constructor, so you type public static MyClass() automatically.

Example of error

public class Logger
{
// ⛔️ Error CS0515: Access modifiers are not allowed on static constructors.
// You cannot make this 'public' because no user code can call it.
public static Logger()
{
Console.WriteLine("Logger initialized");
}
}

Solution: Remove the Modifier

Simply delete the public (or private, protected) keyword. Keep the static keyword.

public class Logger
{
// ✅ Correct: Only 'static' and the class name.
static Logger()
{
Console.WriteLine("Logger initialized");
}
}
note

Why not private? Even private is disallowed. While private implies "only this class uses it," static constructors are conceptually "only the Runtime uses it," which is distinct from private.

Conclusion

CS0515 is a syntax rule derived from the lifecycle of .NET types.

  1. Identify the Constructor: Is it marked static?
  2. Check Modifiers: Does it have public, private, protected, or internal in front of static?
  3. Fix: Delete the access modifier. The definition should start strictly with static ClassName().