You're already familiar with the arithmetic operator symbols from school:
- + for addition
- - for subtraction
- / for division
- * for multiplication
C# operators follow the conventional order of operations, that is, evaluating parentheses first, then exponents, then multiplication, then division, then addition, and finally subtraction (BEDMAS). For instance, the following equations will provide different results, even though they contain the same values and operators:
5 + 4 - 3 / 2 * 1 = 8
5 + (4 - 3) / 2 * 1 = 5
Operators work the same when applied to variables, as they do with literal values.
Assignment operators can be used as a shorthand replacement for any math operation by using any arithmetic and equals symbol together. For example, if we wanted to multiply a variable, both of the following options would produce the same result:
int currentAge = 32;
currentAge = currentAge * 2;
The second, alternative, way to do this is shown here...