In this section we’re assuming you’re familiar with C# operators in general and C# variables.
The assignment operators are used to assign values to variables.
Example:
int iVar = 10;
The = symbol is called assignment operator and is used to assign a value (in this case 10) to a variable.
In the table below you can see the rest of assignment operators:
| Operator | Symbol |
|---|---|
| Assignment operator | = |
| Addition and Assignment operator | += |
| Subtract and Assignment operator | -= |
| Multiplication and Assignment operator | *= |
| Division and Assignment operator | /= |
Now let’s dive deep and see how these operators work:
As mentioned at the beginning of this section, this operator is used to assign a value to a variable. The important note to remember is that, this operator will replace the current content of the variable (if any) with the new value.
Example:
double dVar = 2.033;
dVar = 1000.32;
In this example, the dVar variable is initialized with the value 2.033 via the assignment operator and then we replaced the current value of the variable with the value 1000.32 again via assignment operator. So as you can see, the old and new values of the dVar variable didn’t add to each other but the new one replaced the old one.
Addition and Assignment `+=` Operator:
This is a compound operator which is basically the combination of addition + and assignment = operators.
What it does is that, it will select the value that is already stored in the memory space of the target variable and add it to the new value and after that, stores the result in the variable itself.
Example:
int vary = 100;
vary += 200;
Here the current value of the variable named vary is 100 but then we used the += operator and assigned the value 200. So as a result, the current value (100) is added to the value (200) and the final result is stored in the vary variable. (The final value that is stored in this variable is 300).
Note: the example above could be rewritten like this:
int vary = 100;
vary = vary + 200;
Subtract and Assignment `-=` Operator:
If we want to subtract a value of a variable from another value and store the result in the variable itself, this operator is the way to go.
Basically this operator is the combination of the subtraction - and assignment = operators.
Example:
int vary = 50;
vary -= 30;
The example above could be rewritten like this:
int vary = 50;
vary = vary - 30;
The final value stored in the vary variable for both of the examples above is 20.
Multiplication and Assignment `*=` Operator:
This operator is used to multiply the current value stored in a variable to another value and store the result of the multiplication in the variable itself.
Basically this operator is the combination of the multiplication * and the assignment = operators.
Let’s take a look at an example:
int multiply = 10;
multiply *= 20;
The final value that will be stored in the multiply variable is 200.
What happened is that via *= operator, we’ve multiplied the value stored in the multiply variable which is 10 to the number 20 and assigned the result to the multiply variable.
The example above could be rewritten like this:
int multiply = 10;
multiply = multiply *20;
The result of this example is also 200.
Division and Assignment `/=` Operator:
If we want to quickly divide the value of a variable by another value and assign the result to the variable itself, the /= operator is the perfect candidate.
Let’s take a look at an example:
int division = 100;
division /= 20;
The final value that will be stored in the division variable is 5.
What happened is that via /= operator, we divided the value stored in the division variable which is 100 by the number 20 and assigned the result to the division variable.
The example above could be rewritten like this:
int division = 100;
division = division /20;
The result of this example is also 5.