# C# Assignment Operators

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

Operator | Usage | Description | Examples |
---|---|---|---|

= | A=B | Simple assignment operator. Assign B's value to A. | A = B; //A is 2. |

+= | A+=B | Addition assignment operator. Equivalent to A = A + B | A+=B; //A is 18. |

-= | A-=B | Subtraction assignment operator. Equivalent to A = A - B | A-=B; //A is 14. |

*= | A*=B | Multiplication assignment operator. Equivalent to A = A * B | A*=B; //A is 32. |

/= | A/=B | Division assignment operator. Equivalent to A = A / B | A/=B; //A is 8. |

%= | A%=B | Remainder assignment operator. Equivalent to A = A % B | A%=B; //A is 0. |

&= | A&=B | AND assignment operator. Equivalent to A = A & B | A&=B; //A is 0. |

|= | A|=B | OR assignment operator. Equivalent to A = A | B | A|=B; //A is 18. |

^= | A^=B | Exclusive-OR assignment operator. Equivalent to A = A ^ B | A%=B; //A is 18. |

<<= | A<<=B | Left shift assignment operator. Equivalent to A = A << B | A<<=B; //A is 64. |

>>= | A>>=B | Right shift assignment operator. Equivalent to A = A >> B | A>>=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

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

- 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;