Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Compound assignment operators in C#
Compound assignment operators in C# provide a shorter syntax to perform an operation and assign the result back to the same variable. These operators combine arithmetic, bitwise, or shift operations with assignment in a single step.
For example, x += 5 is equivalent to x = x + 5, but more concise and readable.
Syntax
The general syntax for compound assignment operators is −
variable operator= value;
This is equivalent to −
variable = variable operator value;
Types of Compound Assignment Operators
| Operator | Name | Equivalent Expression |
|---|---|---|
| += | Addition Assignment | x = x + y |
| -= | Subtraction Assignment | x = x - y |
| *= | Multiplication Assignment | x = x * y |
| /= | Division Assignment | x = x / y |
| %= | Modulo Assignment | x = x % y |
| &= | Bitwise AND Assignment | x = x & y |
| |= | Bitwise OR Assignment | x = x | y |
| ^= | Bitwise XOR Assignment | x = x ^ y |
| <<= | Left Shift Assignment | x = x << y |
| >>= | Right Shift Assignment | x = x >> y |
Using Arithmetic Assignment Operators
Example
using System;
class Program {
public static void Main(string[] args) {
int val = 7;
Console.WriteLine("Initial value: " + val);
val += 3; // val = val + 3
Console.WriteLine("After += 3: " + val);
val -= 2; // val = val - 2
Console.WriteLine("After -= 2: " + val);
val *= 7; // val = val * 7
Console.WriteLine("After *= 7: " + val);
val /= 7; // val = val / 7
Console.WriteLine("After /= 7: " + val);
val %= 5; // val = val % 5
Console.WriteLine("After %= 5: " + val);
}
}
The output of the above code is −
Initial value: 7 After += 3: 10 After -= 2: 8 After *= 7: 56 After /= 7: 8 After %= 5: 3
Using Bitwise Assignment Operators
Example
using System;
class Program {
public static void Main(string[] args) {
int val = 12; // Binary: 1100
Console.WriteLine("Initial value: " + val + " (Binary: " + Convert.ToString(val, 2) + ")");
val &= 9; // val = val & 9 (Binary: 1001)
Console.WriteLine("After &= 9: " + val + " (Binary: " + Convert.ToString(val, 2) + ")");
val |= 6; // val = val | 6 (Binary: 0110)
Console.WriteLine("After |= 6: " + val + " (Binary: " + Convert.ToString(val, 2) + ")");
val ^= 3; // val = val ^ 3 (Binary: 0011)
Console.WriteLine("After ^= 3: " + val + " (Binary: " + Convert.ToString(val, 2) + ")");
val >= 3; // val = val >> 3 (shift right by 3)
Console.WriteLine("After >>= 3: " + val + " (Binary: " + Convert.ToString(val, 2) + ")");
}
}
The output of the above code is −
Initial value: 12 (Binary: 1100) After &= 9: 8 (Binary: 1000) After |= 6: 14 (Binary: 1110) After ^= 3: 13 (Binary: 1101) After >= 3: 6 (Binary: 110)
Using Compound Assignment with Strings
The += operator can also be used for string concatenation −
Example
using System;
class Program {
public static void Main(string[] args) {
string message = "Hello";
Console.WriteLine("Initial: " + message);
message += " World"; // message = message + " World"
Console.WriteLine("After += ' World': " + message);
message += "!";
Console.WriteLine("After += '!': " + message);
}
}
The output of the above code is −
Initial: Hello After += ' World': Hello World After += '!': Hello World!
Conclusion
Compound assignment operators in C# provide a concise way to perform operations and assign results simultaneously. They work with arithmetic operations, bitwise operations, shift operations, and even string concatenation, making code more readable and efficient.
