Compiler Error CS0106

The modifier 'modifier' is not valid for this item

A class or interface member was marked with an invalid access modifier. The following examples describe some of these invalid modifiers:

  • The static modifier is not permitted on a local function. The static local function feature is supported starting with C# 8.0. A compiler that doesn't support C# 8.0 produces CS0106 when you try to use this feature. However, a compiler that supports C# 8.0 but the set language version is prior to C# 8.0 will produce a diagnostic suggesting that you use C# 8.0 or later.

  • The public keyword is not allowed on an explicit interface declaration. In this case, remove the public keyword from the explicit interface declaration.

  • The abstract keyword is not allowed on an explicit interface declaration because an explicit interface implementation can never be overridden.

  • Access modifiers are not allowed on a local function. Local functions are always private.

  • The readonly keyword is not allowed on methods in a class type, with the exception of ref readonly returns (readonly keyword must appear after the ref keyword).

In prior releases of Visual Studio, the static modifier was not permitted on a class, but static classes are allowed starting with Visual Studio 2005.

For more information, see Interfaces.

Example

The following sample generates CS0106:

// CS0106.cs
namespace MyNamespace
{
   interface I
   {
      void M1();
      void M2();
   }

   public class MyClass : I
   {
      public readonly int Prop1 { get; set; }   // CS0106
      public int Prop2 { get; readonly set; }   // CS0106

      public void I.M1() {}   // CS0106
      abstract void I.M2() {}   // CS0106

      public void AccessModifierOnLocalFunction()
      {
         public void LocalFunction() {}   // CS0106
      }

      public readonly void ReadonlyMethod() {}   // CS0106
      // Move the `readonly` keyword after the `ref` keyword
      public readonly ref int ReadonlyBeforeRef(ref int reference)   // CS0106
      {
         return ref reference;
      }

      public static void Main() {}
   }

   public readonly class ReadonlyClass {}   // CS0106
}