TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
% 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.
% 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:
% operator adheres to specific rules regarding NaN (Not a Number) and infinity:
- If either operand is
NaN, the result isNaN. - 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 % 0wherexis an integer type throws aDivideByZeroException. - Decimal Division by Zero: Evaluating
x % 0mthrows aDivideByZeroException. - Integer Overflow: Evaluating
int.MinValue % -1(orlong.MinValue % -1L) throws anOverflowException. 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





