C# Reference ยท Operators and Expressions
C# arithmetic operators
Reference for C# arithmetic operators: addition, subtraction, multiplication, division, remainder, integer truncation, overflow and division by zero.
The arithmetic operators are + (addition), - (subtraction), * (multiplication), / (division) and % (remainder). The unary forms +x and -x are arithmetic operators too. They work on all numeric types, and + also concatenates strings and combines delegates.
Operands smaller than int are promoted to int first, so byte + byte is an int. If one operand is wider (long, float, double, decimal), the other is converted to that type. The most surprising rule is that / on two integers is integer division: the fraction is discarded.
int a = 17, b = 5; Console.WriteLine(a / b); // 3 Console.WriteLine(a % b); // 2 Console.WriteLine(a / 5.0); // 3.4
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
result = left + right; result = left - right; result = left * right; result = left / right; result = left % right; result = +value; result = -value;
| Operator | Description |
|---|---|
x + y |
Sum of numbers, string concatenation, or delegate combination. |
x - y |
Difference; also removes a delegate from a delegate chain. |
x * y |
Product. |
x / y |
Quotient. Integer operands give an integer quotient truncated toward zero. |
x % y |
Remainder: x - (x / y) * y for integers. The result has the sign of x. |
-x, +x |
Negation and identity. -x on an int or uint follows the same overflow rules as subtraction. |
Returns: a value of the promoted operand type. int, uint, long, ulong, float, double and decimal have built-in operators; everything smaller is promoted.
Errors: integer / or % by zero throws DivideByZeroException. In a checked context, overflow throws OverflowException. Floating-point division by zero does not throw: it yields Infinity, -Infinity or NaN.
Usage notes
- Integer division truncates toward zero, not toward negative infinity:
-17 / 5is-3. For a fractional result, make one operand floating point (17 / 5.0) or cast it before dividing. - The remainder is not a mathematical modulo.
-1 % 7is-1. Use((x % n) + n) % nwhen you need a result in0..n-1, for example for circular indexes. - Overflow is silent by default for integers. Wrap code in
checked(...)or achecked { }block, or compile with the check-overflow option, when wrapping would be a bug. Constant expressions that overflow are compile errors. int.MinValue / -1cannot be represented and throwsOverflowExceptioneven in unchecked code.%on floating-point types is allowed and returns the remainder with the sign of the left operand.decimalis exact for base-10 fractions (money) but slower;doubleis fast but0.1 + 0.2 != 0.3. Do not mixdecimalwithdoublewithout a cast.+with strings converts the other operand withToString(). Prefer string interpolation in loops and long expressions, andStringBuilderfor many appends.- Compound forms such as
x += 2are covered in assignment operators. Since C# 11, operators can be declared in generic math interfaces (IAdditionOperators<,,>) so one method can add any numeric type. - Performance: all of these are single machine instructions for primitives, except division and remainder, which are noticeably slower than multiplication. The JIT replaces division by constants with cheaper sequences.