Skip to main content

How to Resolve Error "CS0133: The expression being assigned to 'variable' must be constant" in C#

The Compiler Error CS0133 is a definition error regarding the const keyword. The message reads: "The expression being assigned to 'variable' must be constant".

In C#, a const field is evaluated at compile-time. This means the compiler must be able to calculate the exact value of the variable before the program ever runs. If you try to assign a value that requires the program to be executing (like a method call, a runtime calculation, or a non-constant variable), the compiler cannot "bake" the value into the code, resulting in CS0133.

This guide explains the limitations of constants and how to use readonly fields for runtime values.

Understanding Compile-Time Constants

When you declare const int X = 5;, the compiler effectively performs a "Find and Replace" operation. Everywhere you use X in your code, the compiler replaces it with the literal number 5.

Because of this, the value assigned to a const must be:

  • A numeric literal (5, 10.5).
  • A string literal ("Hello").
  • A boolean literal (true, false).
  • null.
  • A mathematical expression using only other constants (5 * 10, "A" + "B").

It cannot be a variable, a function call, or a new object instantiation.

Scenario 1: Assigning Variables or Methods to Constants

The most common mistake is trying to use const for a value that is technically "constant" in logic (it won't change), but requires the runtime engine to calculate it.

Example of error:

using System;

public class TimeSettings
{
// ⛔️ Error CS0133: The expression being assigned to 'StartTime' must be constant.
// DateTime.Now is calculated at RUNTIME. The compiler doesn't know what time it is.
public const DateTime StartTime = DateTime.Now;

public void Calculate()
{
int input = 10;

// ⛔️ Error CS0133: 'input' is a variable. Its value could change.
// Even if it looks fixed here, the compiler treats it as runtime data.
const int result = input * 5;
}
}

Scenario 2: The static readonly Confusion

Developers often confuse static readonly fields (which are initialized once at runtime) with const fields. You cannot use a static readonly variable to initialize a const variable, because readonly is not resolved until the program actually starts.

Example of error:

public class Config
{
// This is calculated at runtime
public static readonly int MaxUsers = 100;

// ⛔️ Error CS0133: 'MaxUsers' is not a compile-time constant.
// The compiler cannot bake 'MaxUsers' into 'Limit'.
public const int Limit = MaxUsers;
}

Solution: Use static readonly instead of const

If the value you need to store is "fixed" but requires calculation (like DateTime.Now, a value from a config file, or a result from another variable), you must change the modifier from const to static readonly.

This tells the compiler: "I don't know the value yet, but I promise to calculate it once when the class loads, and I won't change it after that."

Example of fix:

using System;

public class TimeSettings
{
// ✅ Correct: 'static readonly' allows runtime initialization
public static readonly DateTime StartTime = DateTime.Now;

public void Calculate()
{
int input = 10;

// ✅ Correct: Remove 'const' for local runtime variables
int result = input * 5;
}
}

Fixing the Reference Issue:

public class Config
{
public static readonly int MaxUsers = 100;

// ✅ Correct: Both are now resolved at runtime
public static readonly int Limit = MaxUsers;
}
note

Optimization Detail: const is slightly faster than static readonly because the value is embedded directly into the instruction code. However, static readonly is more flexible and is safer for versioning (referencing libraries don't need to be recompiled if the value changes).

Conclusion

CS0133 enforces the definition of "Constant."

  1. Ask yourself: "Can I write this value down on a piece of paper right now?"
    • If Yes (e.g., 3.14, "Error"), use const.
    • If No (e.g., "Current Time", "Database Result", "Value of Variable X"), use static readonly.
  2. Check References: Ensure you aren't assigning a readonly variable to a const variable.