Compiler Error CS0504

常数“variable”无法标记为 static

如果变量是 const,它也就是 static。如果需要一个既是 const 又是 static 的变量,只需将该变量声明为 const;如果需要的只是 static 变量,只需将其标记为 static

下面的示例生成 CS0504:

// CS0504.cs
namespace x
{
   abstract public class clx
   {
      static const int i = 0;   // CS0504, cannot be both static and const
      abstract public void f();
   }
}