Arithmetic operators
An arithmetic operator is essentially a symbol that performs maths.
They’re usually self-explanatory - the expression 3+4 evaluates to 7 - but
you may not be familiar with all of the symbols used in C#!
Addition
Section titled “Addition”You can do addition with the + operator:
int sum = 3 + 4; // 7double total = 5.5 + 2.3; // 7.8char letter = (char)('A' + 4); // 'E'Note that the + operator can also be used to concatenate (join together)
strings:
string greeting = "Hello, " + "world!"; // "Hello, world!"Its purpose depends on the data types of the values being added together.
Subtraction
Section titled “Subtraction”You can do subtraction with the - operator:
int difference = 10 - 4; // 6double result = 5.5 - 2.3; // 3.2Multiplication
Section titled “Multiplication”You can do multiplication with the * operator (an asterisk, not the times
symbol × or letter x):
int product = 3 * 4; // 12double result = 2.5 * 4.0; // 10.0Order of operations
Section titled “Order of operations”When you have multiple arithmetic operators in a single expression, the order of operations matters!
C# follows the normal mathematical order of operations - you probably know this
as BIDMAS, BODMAS, PEMDAS, or something similar.
In the expression 3 + 4 * 2, the multiplication is done first, so the result
is 3 + 8, which is 11.
You can use parentheses () to change the order of operations. In the
expression (3 + 4) * 2, the addition is done first, so the result is 7 * 2,
which is 14.