Skip to content

How to Resolve Error "CS0220: The operation overflows at compile time in checked mode" in C#

Learn how to fix the CS0220 compiler error in C# when constant expressions exceed integer limits, including type upgrades and the unchecked context.

The Compiler Error CS0220 is a mathematical safety error. The message reads: "The operation overflows at compile time in checked mode".

In C#, the compiler tries to optimize your code by calculating mathematical operations involving constants or literals immediately (a process called "constant folding"). If the compiler detects that the result of such a calculation exceeds the storage capacity of the target data type (e.g., the number is too big for an int), it stops the build to prevent a guaranteed crash or data corruption.

This guide explains how integer limits work and how to resolve this overflow.

Understanding Compile-Time Overflow

Every numeric type in C# has a minimum and maximum value.

  • int (System.Int32): Max value is 2,147,483,647.
  • long (System.Int64): Max value is 9,223,372,036,854,775,807.

If you write const int x = 2147483647 + 1;, the compiler performs the addition. It sees the result (2147483648) fits inside a 64-bit integer but not inside the 32-bit int you requested. Because constant expressions are checked by default in C#, the compiler raises CS0220 instead of silently wrapping the value to a negative number.

Scenario 1: Overflowing Integer Limits

This error occurs when performing math on literals or constants where the result is obviously too large.

Example of error: trying to add 10 to the maximum possible integer value.

public class MathTest
{
    // ⛔️ Error CS0220: The operation overflows at compile time in checked mode
    // The compiler calculates (2147483647 + 10) -> 2147483657.
    // This number is larger than int.MaxValue.
    int number = int.MaxValue + 10; 
}

Solution 1: Use a Larger Data Type

If the result implies a number larger than 2.1 billion, you cannot use int. You must upgrade the variable to long, double, or ulong.

Solution: change the type to long. Note that you usually need to cast one of the operands or use a suffix (L) to tell the compiler to perform the math using 64-bit logic.

public class MathTest
{
    // ✅ Correct: We changed the storage type to 'long'.
    // We also used the 'L' suffix on the literal to ensure the addition 
    // happens in 64-bit space, not 32-bit space.
    long number = int.MaxValue + 10L; 
}

Solution 2: Allow Overflow (Unchecked Context)

Sometimes, you want the value to wrap around (overflow). For example, in hashing algorithms or checksums, MaxValue + 1 is expected to become MinValue.

To permit this, you must explicitly wrap the expression in the unchecked keyword. This tells the compiler: "I know the math doesn't fit; just truncate the bits and give me the result."

Solution:

public class HashCalculator
{
    // ✅ Correct: 'unchecked' suppresses the error.
    // The result wraps around to a negative number.
    public const int WrappedValue = unchecked(int.MaxValue + 10);
}

public class Program
{
    static void Main()
    {
        System.Console.WriteLine(HashCalculator.WrappedValue);
    }
}

Output:

-2147483639

Conclusion

CS0220 is the compiler saving you from a mathematical impossibility.

  1. Check the Type: Can the variable (int, byte, short) hold the result?
  2. Upgrade Type: If you need the large number, switch to long or double.
  3. Allow Wrap: If you intentionally want overflow behavior (e.g., for hash codes), use the unchecked keyword.
Navigation