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

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...