Skip to main content

How to Resolve Error "CS1057: 'member': static classes cannot contain protected members" in C#

The Compiler Error CS1057 is a logical contradiction error involving access modifiers. The message reads: "'member': static classes cannot contain protected members".

In C#, a static class is implicitly sealed, meaning it cannot be inherited from. The protected access modifier is specifically designed to make a member visible to derived classes (children).

Since a static class cannot have children, declaring a member as protected is meaningless and invalid. The compiler flags this because you are trying to define visibility rules for an inheritance hierarchy that cannot exist.

This guide explains the relationship between static classes and inheritance access modifiers.

Understanding Static Class Restrictions

To understand CS1057, recall the definitions:

  • static class: No instances can be created, and no other class can inherit from it.
  • protected: Visible to the containing class AND any classes that inherit from it.

Because inheritance is impossible for a static class, the "AND classes that inherit from it" part of protected is logically impossible. C# enforces this by banning the keyword in this context.

Scenario 1: Using Protected in a Utility Class

This error often occurs when a developer creates a static helper class and instinctively uses protected thinking it means "restricted access" or "internal use," confusing it with private or internal.

Example of error

public static class AppUtilities
{
// ⛔️ Error CS1057: 'AppUtilities.Log(string)': static classes cannot contain protected members
// Who is this protected for? No class can inherit from AppUtilities to see it.
protected static void Log(string message)
{
System.Console.WriteLine(message);
}

public static void Process()
{
Log("Processing...");
}
}

Solution: Choose private or internal

You must decide who needs to see this member.

  • Internal use only: Use private.
  • Project-wide use: Use internal.
  • Public API: Use public.
public static class AppUtilities
{
// ✅ Correct: 'private' restricts it to this class only.
private static void Log(string message)
{
System.Console.WriteLine(message);
}

public static void Process()
{
Log("Processing...");
}
}

Scenario 2: Refactoring Artifacts (Converting to Static)

This error frequently appears during refactoring. You might have a standard class (which supported inheritance) that you decided to make static because it only contained utility methods. However, you forgot to update the access modifiers of the methods inside it.

Example of error

// Previously: public class MathHelpers
// We added 'static' to the class, but forgot to change the methods.
public static class MathHelpers
{
// ⛔️ Error CS1057: This was valid before the class became static.
protected static double Pi = 3.14159;

// ⛔️ Error CS1057
protected static double Square(double x) => x * x;
}

Solution: Audit Access Modifiers

When marking a class as static, you must scan all members. Change any protected members to private (if they were helper methods) or public (if they were intended for external use).

public static class MathHelpers
{
// ✅ Correct: Changed to private (implementation detail)
private static double Pi = 3.14159;

// ✅ Correct: Changed to public (utility method)
public static double Square(double x) => x * x;
}
note

Composite Modifiers: This rule also applies to protected internal and private protected. Since both rely on inheritance as part of their visibility logic, they are both forbidden in static classes.

Conclusion

CS1057 is the compiler reminding you that static classes are leaves on the inheritance tree, not branches.

  1. Analyze Intent: Why did you mark it protected?
  2. Fix Visibility:
    • If it's for internal class logic, make it private.
    • If it's for the assembly, make it internal.
    • If it's for everyone, make it public.