Introduction
Assignment operators are used to assign values to variables. In C#, the most common assignment operator is the simple assignment operator (=
), but there are also several compound assignment operators that combine arithmetic or bitwise operations with assignment.
List of Assignment Operators
- Simple Assignment (=)
- Add and Assign (+=)
- Subtract and Assign (-=)
- Multiply and Assign (*=)
- Divide and Assign (/=)
- Modulus and Assign (%=)
- Bitwise AND and Assign (&=)
- Bitwise OR and Assign (|=)
- Bitwise XOR and Assign (^=)
- Left Shift and Assign (<<=)
- Right Shift and Assign (>>=)
1. Simple Assignment (=)
The simple assignment operator assigns the value of the right-hand operand to the left-hand operand.
Syntax
variable = value;
Example
int a = 5;
Console.WriteLine("a: " + a);
Output:
a: 5
2. Add and Assign (+=)
The add and assign operator adds the value of the right-hand operand to the left-hand operand and assigns the result to the left-hand operand.
Syntax
variable += value;
Example
int a = 5;
a += 3; // Equivalent to a = a + 3;
Console.WriteLine("a: " + a);
Output:
a: 8
3. Subtract and Assign (-=)
The subtract and assign operator subtracts the value of the right-hand operand from the left-hand operand and assigns the result to the left-hand operand.
Syntax
variable -= value;
Example
int a = 5;
a -= 3; // Equivalent to a = a - 3;
Console.WriteLine("a: " + a);
Output:
a: 2
4. Multiply and Assign (*=)
The multiply and assign operator multiplies the left-hand operand by the right-hand operand and assigns the result to the left-hand operand.
Syntax
variable *= value;
Example
int a = 5;
a *= 3; // Equivalent to a = a * 3;
Console.WriteLine("a: " + a);
Output:
a: 15
5. Divide and Assign (/=)
The divide and assign operator divides the left-hand operand by the right-hand operand and assigns the result to the left-hand operand.
Syntax
variable /= value;
Example
int a = 9;
a /= 3; // Equivalent to a = a / 3;
Console.WriteLine("a: " + a);
Output:
a: 3
6. Modulus and Assign (%=)
The modulus and assign operator divides the left-hand operand by the right-hand operand, assigns the remainder to the left-hand operand.
Syntax
variable %= value;
Example
int a = 10;
a %= 3; // Equivalent to a = a % 3;
Console.WriteLine("a: " + a);
Output:
a: 1
7. Bitwise AND and Assign (&=)
The bitwise AND and assign operator performs a bitwise AND operation between the left-hand and right-hand operands and assigns the result to the left-hand operand.
Syntax
variable &= value;
Example
int a = 5; // Binary: 0101
a &= 3; // Binary: 0011; Result: 0001
Console.WriteLine("a: " + a);
Output:
a: 1
8. Bitwise OR and Assign (|=)
The bitwise OR and assign operator performs a bitwise OR operation between the left-hand and right-hand operands and assigns the result to the left-hand operand.
Syntax
variable |= value;
Example
int a = 5; // Binary: 0101
a |= 3; // Binary: 0011; Result: 0111
Console.WriteLine("a: " + a);
Output:
a: 7
9. Bitwise XOR and Assign (^=)
The bitwise XOR and assign operator performs a bitwise XOR operation between the left-hand and right-hand operands and assigns the result to the left-hand operand.
Syntax
variable ^= value;
Example
int a = 5; // Binary: 0101
a ^= 3; // Binary: 0011; Result: 0110
Console.WriteLine("a: " + a);
Output:
a: 6
10. Left Shift and Assign (<<=)
The left shift and assign operator shifts the bits of the left-hand operand left by the number of positions specified by the right-hand operand and assigns the result to the left-hand operand.
Syntax
variable <<= numberOfPositions;
Example
int a = 5; // Binary: 0101
a <<= 1; // Binary: 1010
Console.WriteLine("a: " + a);
Output:
a: 10
11. Right Shift and Assign (>>=)
The right shift and assign operator shifts the bits of the left-hand operand right by the number of positions specified by the right-hand operand and assigns the result to the left-hand operand.
Syntax
variable >>= numberOfPositions;
Example
int a = 5; // Binary: 0101
a >>= 1; // Binary: 0010
Console.WriteLine("a: " + a);
Output:
a: 2
Combining Assignment Operators
Assignment operators can be combined with arithmetic or bitwise operations to perform compound operations.
Example: Using Multiple Assignment Operators
int a = 10;
a += 2; // a = a + 2
a *= 3; // a = a * 3
a -= 4; // a = a - 4
a /= 2; // a = a / 2
Console.WriteLine("a: " + a);
Output:
a: 14
Example: Using Bitwise Assignment Operators
int a = 5; // Binary: 0101
a &= 3; // Binary: 0011; Result: 0001
a |= 4; // Binary: 0100; Result: 0101
a ^= 2; // Binary: 0010; Result: 0111
Console.WriteLine("a: " + a);
Output:
a: 7
Conclusion
Understanding and using assignment operators is crucial for efficiently performing calculations and managing data in your C# programs. The simple assignment operator (=
) and the compound assignment operators (+=
, -=
, *=
, /=
, %=
) allow you to perform arithmetic and bitwise operations while assigning the result back to the variable. By mastering these operators, you can write more concise and readable code.