Skip to main content

How to Resolve Error "CS0463: Evaluation of the decimal constant expression failed" in C#

The Compiler Error CS0463 is a mathematical error that occurs at compile-time. The message reads: "Evaluation of the decimal constant expression failed".

In C#, if you declare a variable as const, the compiler attempts to calculate its value immediately during the build process (a technique known as "Constant Folding"). If the result of that calculation is too large to fit in the decimal data type (overflow), or involves an illegal operation (like dividing by zero), the compiler cannot generate the constant value and raises this error.

This guide explains the limits of compile-time math and how to move these calculations to runtime to resolve the error.

Understanding Compile-Time Evaluation

When you write const decimal x = 5 * 2;, the compiler doesn't generate code to multiply 5 by 2. Instead, it calculates 10 internally and bakes the number 10 directly into your compiled DLL.

However, the decimal type has a specific range ($\pm 7.9 \times 10^28$) and strict rules.

  • Double (double): If you overflow a constant double, it becomes Infinity. This is allowed.
  • Decimal (decimal): If you overflow a constant decimal, it is an Error. C# does not support "Decimal Infinity".

Scenario 1: Decimal Overflow

This occurs when you perform multiplication or addition on large decimal literals, and the result exceeds decimal.MaxValue.

Example of error: attempting to double the maximum possible decimal value.

public class FinancialCalc
{
// Max decimal is approx 79,228,162,514,264,337,593,543,950,335
public const decimal MaxValue = 79228162514264337593543950335m;

// ⛔️ Error CS0463: Evaluation of the decimal constant expression failed.
// The compiler tries to compute (MaxValue * 2), sees that it won't fit,
// and stops the build.
public const decimal ImpossibleValue = MaxValue * 2;
}
note

If this were const double, the result would be double.PositiveInfinity, which compiles successfully. decimal does not allow this.

Scenario 2: Divide by Zero

While less common with literals (because it's obvious), this can happen if you use chained constants where one evaluates to zero.

Example of error:

public class Rates
{
public const decimal BaseRate = 0.0m;

// ⛔️ Error CS0463: Evaluation failed.
// The compiler attempts 100.0 / 0.0.
public const decimal CalculatedRate = 100.0m / BaseRate;
}

Solution: Use static readonly

If the calculation is legitimate but potentially large (or if you want to handle the overflow exception at runtime rather than preventing the build), or if you simply need to bypass the compiler's strict check, change the modifier from const to static readonly.

This defers the calculation until the program starts (Runtime).

Solution

public class FinancialCalc
{
public const decimal MaxValue = 79228162514264337593543950335m;

// ✅ Correct: 'static readonly' is calculated at runtime.
// NOTE: This will compile, but it will throw 'System.OverflowException'
// when you run the application.
public static readonly decimal RuntimeCalculation = MaxValue * 2;
}

Why this fixes the compiler error

By removing const, you stop the compiler from acting as a calculator. The compiler generates code that says "Please calculate this when the app launches." The compiler is happy because it doesn't have to deal with the math itself.

warning

Runtime Crash: Using static readonly fixes the compiler error (CS0463), but if the math results in an overflow, your application will crash with an OverflowException upon initialization unless wrapped in a try-catch block inside a static constructor.

Conclusion

CS0463 is the compiler telling you: "I calculated this math, and the result is not a valid number."

  1. Check the Math: Are you dividing by zero? Are the numbers unreasonably large?
  2. Check the Type: Does this need to be decimal? double handles extremely large numbers better (at the cost of precision).
  3. Defer to Runtime: Change const to static readonly. This allows the code to compile, though you must still ensure the math is valid to avoid runtime exceptions.