How to Resolve Error "CS0106: The modifier is not valid for this item" in C#
The Compiler Error CS0106 is a syntax and semantic error. The message reads: "The modifier 'public' (or static, virtual, etc.) is not valid for this item".
In C#, keywords (modifiers) like public, private, static, virtual, override, and async are context-sensitive. You cannot apply every keyword to every type of declaration. For example, you cannot make a static method virtual, nor can you apply an access modifier to a static constructor.
This guide covers the most common scenarios where developers apply invalid modifiers and how to correct them.
Understanding Modifiers
Modifiers control Access (e.g., public, private), Inheritance (e.g., virtual, abstract), and Behavior (e.g., static, async). The C# language specification dictates strictly where these can be used.
If you try to use a modifier in a context that contradicts the language rules (like trying to define a private method inside an interface declaration without a body), the compiler throws CS0106.
Scenario 1: Modifiers in Interfaces
Historically, members of an interface are implicitly public and abstract. Adding the public keyword to a standard interface member declaration is redundant and, in older C# versions (or specific contexts), invalid.
Example of error
Trying to explicitly declare an interface method as public.
public interface ILogger
{
// ⛔️ Error CS0106: The modifier 'public' is not valid for this item
// (In standard interface definitions, visibility is implicit).
public void Log(string message);
}
Solution
Remove the access modifier.
public interface ILogger
{
// ✅ Correct: Implicitly public
void Log(string message);
}
C# 8.0+ Update: Modern C# allows access modifiers in interfaces if you provide a default implementation (a method body). However, for standard abstract definitions (signatures only), modifiers should generally be omitted.
Scenario 2: Static Constructors
A Static Constructor (static ClassName()) is called automatically by the CLR (Common Language Runtime) to initialize static data. Because the runtime calls it, not your code, you cannot specify access modifiers like public or private.
Example of error
public class Database
{
// ⛔️ Error CS0106: The modifier 'public' is not valid for this item
// You cannot control who calls a static constructor; the runtime does it.
public static Database()
{
Console.WriteLine("Database initialized");
}
}
Solution
Remove the access modifier completely. Keep the static keyword.
public class Database
{
// ✅ Correct: No access modifier allowed
static Database()
{
Console.WriteLine("Database initialized");
}
}
Scenario 3: Local Functions
Local functions are methods defined inside other methods. Because they live entirely within the scope of the outer method, they do not support access modifiers like public or private. They are implicitly private to that scope.
Example of error
public void ProcessData()
{
// ⛔️ Error CS0106: The modifier 'public' is not valid for this item
// This function only exists inside ProcessData.
public int Add(int a, int b)
{
return a + b;
}
int result = Add(5, 5);
}
Solution
Remove the visibility modifier. (Note: You can use static or async on local functions).
public void ProcessData()
{
// ✅ Correct: Implicitly visible only to ProcessData
int Add(int a, int b)
{
return a + b;
}
// ✅ Correct: 'static' is valid for local functions (prevents capturing variables)
static int Multiply(int a, int b) => a * b;
int result = Add(5, 5);
}
Scenario 4: Combining Mutually Exclusive Keywords
Certain modifiers logically contradict each other. For example, static means "belongs to the type, not the instance," while virtual means "can be overridden by a derived instance." Therefore, a method cannot be both.
Example of error
public class BaseClass
{
// ⛔️ Error CS0106: The modifier 'virtual' is not valid for this item
// Static members cannot be virtual, abstract, or overridden.
public static virtual void Show()
{
}
}
Solution
Decide on the behavior. Is it a utility method (Static)? or a polymorphic method (Virtual)?
public class BaseClass
{
// ✅ Correct: It is an instance method, so it can be virtual
public virtual void Show()
{
}
// OR
// ✅ Correct: It is a static utility method
public static void ShowStatic()
{
}
}
Conclusion
CS0106 acts as a grammar check for your C# declarations.
- Interfaces: Usually, do not add
publicto method signatures. - Static Constructors: Never add
publicorprivate. - Local Functions: Never add
publicorprivate. - Static Members: Never add
virtual,abstract, oroverride.