C# Assignment Operators

Assignment operators are shown below and assume A=16 and B=2 in the examples.

OperatorUsageDescriptionExamples
=A=BSimple assignment operator. Assign B's value to A.A = B;  //A is 2.
+=A+=BAddition assignment operator. Equivalent to A = A + BA+=B;  //A is 18.
-=A-=BSubtraction assignment operator. Equivalent to A = A - BA-=B;  //A is 14.
*=A*=BMultiplication assignment operator. Equivalent to A = A * BA*=B;  //A is 32.
/=A/=BDivision assignment operator. Equivalent to A = A / BA/=B;  //A is 8.
%=A%=BRemainder assignment operator. Equivalent to A = A % BA%=B;  //A is 0.
&=A&=BAND assignment operator. Equivalent to A = A & BA&=B;  //A is 0.
|=A|=BOR assignment operator. Equivalent to A = A | BA|=B;  //A is 18.
^=A^=BExclusive-OR assignment operator. Equivalent to A = A ^ BA%=B;  //A is 18.
<<=A<<=BLeft shift assignment operator. Equivalent to A = A << BA<<=B;  //A is 64.
>>=A>>=BRight shift assignment operator. Equivalent to A = A >> BA>>=B;  //A is 4.
  • Simple assignment operator "=" assigns the right operand value to the left operand.
  • The above operators except "=" are called compound assignment operators.
  • Compound assignment operators are used to shorten the assignment if the first right operand is the same as the left one.
  • All the assignment operators are calculated from right to left.

We'll check the results in the above examples

Example 01-12-01

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;

namespace TestAssignmentOperators
{
    class Program
    {
        static void Main(string[] args)
        {
            int a=16, b=2;
            a = b;
            Console.WriteLine("After a = b, then a = {0}", a);
            a = 16; a += b;
            Console.WriteLine("After a += b, then a = {0}", a);
            a = 16; a -= b;
            Console.WriteLine("After a -= b, then a = {0}", a);
            a = 16; a *= b;			
            Console.WriteLine("After a *= b, then a = {0}", a);
            a = 16; a /= b;
            Console.WriteLine("After a /= b, then a = {0}", a);
            a = 16; a %= b;
            Console.WriteLine("After a %= b, then a = {0}", a);
            a = 16; a &= b;
            Console.WriteLine("After a &= b, then a = {0}", a);
            a = 16; a |= b;
            Console.WriteLine("After a |= b, then a = {0}", a);
            a = 16; a ^= b;
            Console.WriteLine("After a ^= b, then a = {0}", a);
            a = 16; a <<= b;
            Console.WriteLine("After a <<= b, then a = {0}", a);
            a = 16; a >>= b;
            Console.WriteLine("After a >>= b, then a = {0}", a);
			
            int c = a = b;
            Console.WriteLine("a={0}, b={1}, c={2}", a, b, c);
            Console.Read();
        }
    }
}

Output

After a = b, then a = 2
After a += b, then a = 18
After a -= b, then a = 14
After a *= b, then a = 32
After a /= b, then a = 8
After a %= b, then a = 0
After a &= b, then a = 0
After a |= b, then a = 18
After a ^= b, then a = 18
After a <<= b, then a = 64
After a >>= b, then a = 4
a=2, b=2, c=2
  • Line 9-31: The result is exactly the same as that in the above table. Each time we'll assign 16 to a because a has been changed after each assignment.
  • Line 33: Because the assignment operator runs from right to left. First b assigns to a and returns b's value. then the value assigns to c.
  • Line 34: Output a, b c with the same value;