Skip to main content
The % operator in C# is the remainder operator. It computes the remainder resulting from the division of its left-hand operand (dividend) by its right-hand operand (divisor).

Type Support and Numeric Promotion

The % operator is predefined for the following standard numeric types:
  • Integer types: int, uint, long, ulong.
  • Floating-point types: float, double.
  • High-precision decimal: decimal.
When the % operator is applied to smaller integral types (sbyte, byte, short, ushort), C# applies implicit numeric promotion. The operands are automatically converted to int before the operation is evaluated, and the resulting remainder is of type int.

Sign Determination Rules

In C#, the sign of a non-zero remainder is strictly determined by the left-hand operand (the dividend). The sign of the right-hand operand (the divisor) has no effect on the sign of the result.

Floating-Point Mechanics

Unlike some C-family languages, C# natively supports the % operator for floating-point types (float and double). For finite values, the remainder is conceptually calculated by truncating the intermediate quotient towards zero. Due to standard IEEE 754 floating-point precision mechanics, using float or double can result in minor precision artifacts. To achieve exact base-10 fractional remainders, the decimal type must be used:
When evaluating floating-point operands, the % operator adheres to specific rules regarding NaN (Not a Number) and infinity:
  • If either operand is NaN, the result is NaN.
  • If the dividend is infinity, the result is NaN.
  • If the divisor is zero, the result is NaN (no exception is thrown).
  • If the dividend is finite and the divisor is infinity, the result is the dividend.

Exceptions and Edge Cases

The behavior of the % operator under exceptional conditions depends entirely on the numeric types involved:
  • Integer Division by Zero: Evaluating x % 0 where x is an integer type throws a DivideByZeroException.
  • Decimal Division by Zero: Evaluating x % 0m throws a DivideByZeroException.
  • Integer Overflow: Evaluating int.MinValue % -1 (or long.MinValue % -1L) throws an OverflowException. This occurs because the underlying division operation (int.MinValue / -1) results in a value that exceeds the maximum bounds of the signed integer type.
Tired of Poor C# Skills? Fix That With Deep Grasping!Learn More