How to Resolve Error "CS0504: The constant 'variable' cannot be marked static" in C#
The Compiler Error CS0504 is a syntax restriction error. The message reads: "The constant 'variable' cannot be marked static".
In C#, the const keyword implicitly defines a member as static. A constant value belongs to the type itself, not to a specific instance of the class. Because "static behavior" is already built into the definition of a constant, C# forbids you from explicitly adding the static keyword. It is considered redundant and syntactically incorrect.
This guide explains how constants work in C# and how to declare them correctly.
Understanding Implicit Static Behavior
When you declare a member as const, the compiler treats it as a fixed value shared by all instances of the class. It is stored in the assembly's metadata.
- Instance Access: You generally cannot access a constant via an object variable (e.g.,
myObj.MyConst). - Static Access: You access it via the class name (e.g.,
MyClass.MyConst).
Since the behavior is already static, adding the keyword static causes a conflict in the language grammar.
Scenario: The Redundant Keyword
This error commonly occurs when developers want to ensure a value is global and shared, so they habitually add static to the declaration, not realizing const handles this automatically.
Example of error:
public class MathConstants
{
// ⛔️ Error CS0504: The constant 'Pi' cannot be marked static.
// 'const' is ALREADY static. You cannot say it twice.
public static const double Pi = 3.14159;
}
Solution 1: Remove the static Keyword
To fix the error, simply delete the static modifier. The way you access the variable remains exactly the same.
Solution:
public class MathConstants
{
// ✅ Correct: implicit static behavior
public const double Pi = 3.14159;
}
public class Program
{
static void Main()
{
// Accessing it via the Class Name (Proving it is static)
double area = MathConstants.Pi * 10 * 10;
System.Console.WriteLine(area);
}
}
Solution 2: Use static readonly (For Runtime Values)
If you added static because the value isn't actually a compile-time constant (e.g., it involves a method call, a new object, or a runtime calculation), then you shouldn't be using const at all. You should use static readonly.
static readonly allows you to keep the static keyword explicitly.
Solution:
public class AppConfig
{
// ⛔️ Error CS0504 (and CS0133): DateTime.Now is not constant.
// public static const DateTime StartTime = DateTime.Now;
// ✅ Correct: Use 'static readonly' for runtime static values.
public static readonly DateTime StartTime = DateTime.Now;
}
Quick Comparison:
const: Compile-time literal. Implicitly static. Cannot usestatickeyword.static readonly: Runtime value. Explicitly static. Must usestatickeyword.
Conclusion
CS0504 is the compiler telling you not to repeat yourself.
- If it is a literal (
5,"hello"): Usepublic const Type Name = Value;. Removestatic. - If it is an object or calculation: Use
public static readonly Type Name = Value;. Keepstatic.