Assignment Operators are shortcuts for modifying the value of a variable and immediately assigning the result to the variable itself. It is used to modify the value of the left operand depending on the right operand. The table shows assignment operators and their functionality.

Operator Example Expression Result
= var1 = var2; var1 is assigned a value of var2.
+= var1 += var2; var1 is assigned the value that is the sum of
var1 and var2
-= var1 -= var2; var1 is assigned the value that is the difference of
var1 and var2
*= var1 *= var2; var1 is assigned the value that is the product of
var1 and var2.
/= var1 /= var2; var1 is assigned the value that is the result of
dividing var1 by var2
%= var1 %= var2; var1 is assigned the value that is the remainderr
when var1 is divided by var2

The += can also be used for concatenating strings. The table above shows that the assignment operators are actually a short hand of doing calculations on two operands. Consider var1 += var2, the original form of it would be var1 = var1 + var2. It would be more efficient to use assignment operators specially when you have very long variable names.

The following program shows how to use assignment operators and their effect on variables. Since the concept for each of the assignment operators is the same, I’m not going to show all of them.

using System;
 
public class Program
{
    public static void Main()
    {
        int number;
 
        Console.WriteLine("Assigning 10 to number...");
        number = 10;
        Console.WriteLine("Number = {0}", number);
 
        Console.WriteLine("Adding 10 to number...");
        number += 10;        
        Console.WriteLine("Number = {0}", number);
 
        Console.WriteLine("Subtracting 10 from number...");
        number -= 10;
        Console.WriteLine("Number = {0}", number);
    }
}

Example 1

Assigning 10 to number...
Number = 10
Adding 10 to number...
Number = 20
Subtracting 10 from number...
Number = 10

The program uses three of the assignment operators. First, we declared a variable that will hold the value for manipulation. We then assign a value of 10 using the = operator. We added 10 to the number by using the += operator. Finally, we subtracted 10 from the number using -= operator.