HomeCSharpC# Error CS0463 – Evaluation of the decimal constant expression failed with error: ‘error’

C# Error CS0463 – Evaluation of the decimal constant expression failed with error: ‘error’

C# Compiler Error

CS0463 – Evaluation of the decimal constant expression failed with error: ‘error’

Reason for the Error

You’ll get this error in your C# code when you are using the decimal expression and it overflows at the compile time.

For example, let’s try to compile the below C# code snippet.

using System;
namespace DeveloperPublishNamespace
{
    class Program
    {      
        static void Main(string[] args)
        {
            decimal value1 = 79000000000000000000000000000.0m + 79000000000000000000000000000.0m;
            Console.WriteLine(value1.ToString());
        }

    }
}

You’ll receive the error code CS0463 when you build the above C# code because the C# compiler has detected that the expression that adds the two decimal values results in decimal overflow.

Error CS0463 Evaluation of the decimal constant expression failed DeveloperPublish C:\Users\Senthil\source\repos\ConsoleApp4\ConsoleApp4\Program.cs 8 Active

C# Error CS0463 – Evaluation of the decimal constant expression failed with error: 'error'

If you are running an older version of Visual Studio or C#, you may see a different error as shown below.

Main.cs(8,30): error CS0220: The operation overflows at compile time in checked mode
Compilation failed: 1 error(s), 0 warnings

If you use the variables instead of constant, you will not receive the compile time error. However, you’ll end up getting the run time error. For example, try to compile and run the below code snippet to see the runtime error in action.

using System;
namespace DeveloperPublishNamespace
{
    class Program
    {      
        static void Main(string[] args)
        {
            decimal value1 = 79000000000000000000000000000.0m;
            decimal value2 = 79000000000000000000000000000.0m;
            decimal myDec = value1 +value2; 
            Console.WriteLine(myDec.ToString());
        }

    }
}

System.OverflowException: ‘Value was either too large or too small for a Decimal.’

Solution

You can fix this error in your C# program by avoiding the constant expression that causes the error.

Leave a Reply

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...