Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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).
var result = dividend % 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.
byte a = 5;
byte b = 3;
var result = a % b; // result is of type int, not byte

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.
Console.WriteLine( 5 %  3);  // Output:  2
Console.WriteLine(-5 %  3);  // Output: -2
Console.WriteLine( 5 % -3);  // Output:  2
Console.WriteLine(-5 % -3);  // Output: -2

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:
Console.WriteLine(5.5m % 2.2m); // Output: 1.1 (Exact decimal evaluation)
Console.WriteLine(5.5 % 2.2);   // Output: 1.0999999999999996 (IEEE 754 double precision)
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.
Console.WriteLine(5.0 % double.PositiveInfinity); // Output: 5
Console.WriteLine(double.PositiveInfinity % 5.0); // Output: NaN
Console.WriteLine(5.0 % 0.0);                     // Output: NaN

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.
Master C# with Deep Grasping Methodology!Learn More