Skip to main content

How to Resolve Error "CS0020: Division by constant zero" in C#

The Compiler Error CS0020 is a logic error detected during the build process. The message reads: "Division by constant zero".

This error occurs when the compiler analyzes an arithmetic operation and determines that the denominator (the bottom number) is the literal number 0 or a constant variable holding 0. Because division by zero is mathematically undefined (and crashes programs instantly), the C# compiler prevents you from building code that contains this guaranteed failure in integer or decimal arithmetic.

This guide explains the difference between compile-time checks and runtime exceptions for division by zero.

Understanding Compile-Time vs. Runtime Division Errors

It is important to distinguish CS0020 from the standard System.DivideByZeroException.

  • CS0020 (Compiler Error): The compiler knows the value is zero before the program ever runs. This happens with literals (0) or constants (const int).
  • DivideByZeroException (Runtime Error): The compiler cannot know the value (e.g., user input or a calculated variable). The code compiles, but the app crashes when it hits that line.

CS0020 is the compiler saving you from writing code that is 100% guaranteed to crash.

Scenario 1: Direct Integer Division

The most obvious cause is typing 0 directly into an equation involving integers (int, long, short, byte) or decimal types.

Example of Mistake

public class Calculator
{
public void Calculate()
{
int total = 100;

// ⛔️ Error CS0020: Division by constant zero
// The compiler sees '0' and blocks the build.
int average = total / 0;
}
}

Solution

Ensure the divisor is non-zero. If this was a placeholder for logic to be written later, replace it with a valid number or a variable.

public void Calculate()
{
int total = 100;
int count = 1; // Or a variable input

// ✅ Correct: Division by a valid number or variable
int average = total / count;
}

Scenario 2: Constant Variables

The error also triggers if you use a const variable. Because const values are substituted at compile-time (like find-and-replace), the compiler sees the zero just as clearly as if you typed the literal 0.

Example of Mistake

public class Config
{
// A configuration constant set to zero
public const int DefaultStepSize = 0;
}

public class Processor
{
public void Run()
{
int distance = 500;

// ⛔️ Error CS0020: 'DefaultStepSize' is a constant 0.
// The compiler resolves this to "500 / 0".
int steps = distance / Config.DefaultStepSize;
}
}

Non-Constant Variables

If you remove the const keyword, the error disappears (but the danger remains).

public class Config
{
// Not a constant anymore, just a standard static field
public static int DefaultStepSize = 0;
}

public class Processor
{
public void Run()
{
// ⚠️ Compiles Successfully, BUT throws 'DivideByZeroException' at runtime!
// The compiler doesn't check values of standard variables.
int steps = 500 / Config.DefaultStepSize;
}
}

Scenario 3: Floating-Point Exception (Infinity)

You might notice that dividing by zero behaves differently for floating-point numbers (float and double). According to IEEE 754 standards, 1.0 / 0.0 is not an error; it results in Infinity.

Therefore, CS0020 is not raised for float or double literals, but it is raised for decimal (which enforces precise base-10 arithmetic).

public void FloatingPointTest()
{
// ✅ Allowed: Results in Double.PositiveInfinity
double d = 10.0 / 0.0;
System.Console.WriteLine(d); // Output: Infinity

// ✅ Allowed: Results in Float.NaN (Not a Number)
float f = 0.0f / 0.0f;

// ⛔️ Error CS0020: Decimal does not support Infinity.
decimal m = 10.0m / 0.0m;
}

Conclusion

CS0020 is a safety mechanism for integer and decimal arithmetic.

  1. Check your math: Look for literal 0s in denominators.
  2. Check constants: Ensure const variables used in division are not set to zero.
  3. Validate inputs: If you switch to using variables to bypass this error, remember to wrap your division in if (divisor != 0) blocks to prevent the inevitable runtime crash.