In this section we’re assuming you already read the C# operators section.
Those symbols that are used to run basic mathematical operations are called arithmetic operators and we can use them to do such operations in a C# program.
In the table below you can see this list of mathematical operators.
| Operator | Symbol |
|---|---|
| Addition Operator | + |
| Subtraction Operator | – |
| Multiplication Operator | * |
| Division Operator | / |
| Modulus Operator | % |
| Increment Operator | ++ |
| Decrement Operator | — |
The addition operator takes two operands (values) and adds them together. These operands can be a variable, constant values (like 1.22, 2, 3,4 etc.) or the result of expression or the returned value from a method call.
Example:
using System;
namespace quickExample
{
public class Program
{
public static void Main(string[] args)
{
int integer = 10;
int result = 2 + integer;
Console.WriteLine(result);
}
}
}
Output: 12;
Subtraction `-` Operator:
This operator is on the contrary of the addition operator and is used to subtract two operands (values) from each other. Just like addition operator the operands can be either a constant value like (10, 4 ,2, 43.22 etc.), the result of calling a method or the value of a variable.
Example:
using System;
namespace quickExample
{
public class Program
{
public static void Main(string[] args)
{
int integer = 10;
int result = 2 - integer;
Console.WriteLine(result);
}
}
}
Output:
-8
Multiplication `*` operator:
When we want to multiply two values to each other, we use multiplication * operator. The values that can be used as the operands of the multiplication-operator can be the value assigned to a variable, constant values or the result of a method call.
Example:
using System;
namespace quickExample
{
public class Program
{
public static void Main(string[] args)
{
int integer = 20;
int result = integer * integer;
Console.WriteLine(result);
}
}
}
Output: 400
Division `/` operator:
On the contrary to the multiplication operator, this operator is used to divide the operand on the left side by the operand on the right side of the division / operator. Just like multiplication operator, the values that are applicable to this operator are constant values, the value of variables and the returned value from calling a method and also the result of an expression.
Example:
using System;
namespace quickExample
{
public class Program
{
public static void Main(string[] args)
{
int vOne = 100;
int vTwo = 200;
int result = vTwo / vOne;
Console.WriteLine(result);
}
}
}
Output: 2
Modulus `%` Operator:
Let me ask a question: how many 3 are in 10?
If you look carefully, it is 3. This is because if we multiply 3 to 4 we will get 12 which are two values over the number 10 but if we multiply 3 by 3 the result is 9. Now if we subtract the value 10 from 9 what’s the remainder? It is 1.
We use modulus operator to get that remainder.
Example:
using System;
namespace quickExample
{
public class Program
{
public static void Main(string[] args)
{
int result = 10 % 3;
Console.WriteLine(result);
}
}
}
Output:
1
Increment `++` Operator:
When we want to increase the value of a variable by one and don’t care what is the current value of that variable, we can use the increment ++ operator.
This operator can be set on the left or right side of the target variable.
Example:
using System;
namespace quickExample
{
public class Program
{
public static void Main(string[] args)
{
int valueRight = 13;
int valueLeft = 13;
int left = ++valueLeft;
int right = valueRight++;
Console.WriteLine("left-variable: " + left + ", right-variable: " + right + ", valueRight: " + valueRight);
}
}
}
Output:
left-variable: 14 , right-variable: 13, valueRight: 14
Here's the difference:
If the ++ operator appeared on the left side of a variable like int left = ++valueLeft; that means first increase the value assigned to the operand (in this case valueLeft) and then proceed to the rest of operations. So here the assigned value to the left variable will be 14.
On the other hand, if the ++ operator appeared on the right side of a variable like int right = valueRight++;, the value assigned to the operand (in this case valueRight) will be increased by one, but this is only guaranteed for the next time we call the target variable. So the final value that is assigned to the variable right in this example is 13 because the value of the valueRight did not change right away and the right variable took the old value stored in this variable.
Decrement `–` Operator:
This operator is used to decrease the value assigned to a variable by one.
Notes:
-
This operator can be set on the left and right side of the target
variable. -
Rules that applied to `++` operator also apply to `–` operator
as well.
Example:
using System;
namespace quickExample
{
public class Program
{
public static void Main(string[] args)
{
int valueRight = 13;
int valueLeft = 13;
int left = --valueLeft;
int right = valueRight-- + valueRight;
Console.WriteLine("left-variable: " + left + ", right-variable: " + right + ", valueRight: " + valueRight);
}
}
}
Output:
left-variable: 12, right-variable: 25, valueRight: 12
Note: The basic arithmetic operations mentioned in this section apply to operands from left to right.
For example if we have int res = 10/2; the value 10 is divided by 2 and not vice versa!