Arithmetic and Precedence
Operators and order of evaluation.
Arithmetic Operators
C# provides the familiar arithmetic operators:
+addition-subtraction*multiplication/division%remainder (modulo)
They work on every numeric type.
int a = 10, b = 3;
Console.WriteLine(a + b);
Console.WriteLine(a - b);
Console.WriteLine(a * b);Integer Division
When both operands are integers, / performs integer division and discards the fractional part.
So 7 / 2 is 3, not 3.5. This is a common source of beginner bugs.
int result = 7 / 2;
Console.WriteLine(result);All lessons in this course
- Numeric Types
- Arithmetic and Precedence
- The Math Class
- Converting Between Numbers