Assignment Operators in c#
Assignment operators in the C# programming language are used to assign a user-defined numeric, text, or Boolean value to the declared variables of the parameter data type in a C# program. Assignment operators in the C# programming language are mostly used to combine assignment data type values with arithmetic or bitwise data type operator operations.

List of popular assignment operators in the C# programming language.
| Operator | Operator Name | Assignment Description | illustration | Similar To |
| = | Assignment symbol | It Used to Assigns the right-hand value to the user define variable | p = 7 | p = 7 |
| += | Add and assign | It used to Adds numeric value and assigns the output value | p += 3 | p = p + 3 |
| -= | Subtract and assign | It used to Subtracts numeric value and assigns the output | p -= 9 | p = p – 9 |
| *= | Multiply and assign | It used to Multiplies numeric value and assigns the final output value | p *= 8 | p = p * 8 |
| /= | Divide and assign | It used to Divides and assigns the numeric division value to output | p /= 5 | p = p / 5 |
| %= | Modulus and assign | It used to Computes remainder value and assigns the value to output | p %= 4 | p = p % 4 |
| &= | Bitwise AND and assign | It used to Performs bitwise AND operator and assigns the output value | p &= 2 | P =p & 2 |
| | | Bitwise OR and assign | It used to Performs Bitwise OR and assign the output value | p |= 7 | p = p | 7 |
| ^= | Bitwise XOR and assign | It used to Performs bitwise XOR and assigns the value to output | p ^= 2 | p = p ^ 2 |
| <<= | Left shift and assign | It used to Performs Shifts bits to the left and assigns value to output | p <<= 3 | p = p << 3 |
| >>= | Right shift and assign | It used to Performs Shifts bits to the right and assigns value to output | p >>= 2 | p = p >> 2 |
Assignment operator examples in C#.
using System;
class AssignmentOperator
{
static void Main()
{
int p = 21;
p += 4; // This is similar to p = p + 4
Console.WriteLine($”p variable value after += operation – {p}”); // Result is – 25
p *= 3; // This is similar to p = p * 3
Console.WriteLine($”p variable value after *= operation – {p}”); // Result is – 75
p %= 2; // this is similar to p = p % 2
Console.WriteLine($”p variable value after %= operation – {p}”); // Result is – 1
p|= 2; // this is similar to p Bitwise OR operation (p = p | 2)
Console.WriteLine($”p variable value after |= operation – {p}”); // Result is – 3
}
}
Bitwise OR and Bitwise XOR examples in C#.
Using System;
class BitwiseORandBitwiseXOR
{
static void Main()
{
int p=9; // equivalent to Binary value – 1001
int q = 7; // equivalent to Binary value – 0111
// here we use Bitwise OR operator
int output = p | q;
Console.WriteLine(output); // Result is – 15
// here we use XOR Assignment operator
p^=q; // similar as – p = p ^ q
Console.WriteLine(p); // Result is – 14
}
}
Left shift and assign Right shift examples in C#.
Using System;
class LeftshiftandassignRightshift
{
static void Main()
{
int q = 8; // Binary equivalent value – 1000
// here we use Left Shift Assignment operator
q <<= 2; // similar as – q = q << 2
Console.WriteLine(q); // Result is – 32
// here we use Right Shift Assignment operator
q >>= 1; // similar as – q = q >> 1
Console.WriteLine(q); // Result is – 16
}
}
Important Notes in the C# Programming Language.
C# Chained Assignment Concept.
In a C# program, multiple parameter variables can be assigned the same value or assigned the same custom value within a single user-defined statement. They can be processed based on user-defined logic expressions within the program.
using System;
class ChainedAssignments
{
static void Main()
{
/ Here we are assigning the same value to multiple similar variables
int p, q, r;
p = q = r = 7;
Console.WriteLine(p + q + r); // Result is – 21
}
}
Assignment operators (+=, -=) are used in for loops.
In a C# program, you can use the += and -= assignment operators to traverse, iterate, or repeat the loop values from the start to the end of a for loop as needed.
using System;
class AssignWithForloop
{
static void Main()
{
for (int p = 1; p <= 21; p += 3)
{
Console.WriteLine(p); // Output is – 1, 4 7, 10, 13, 16, 19
}
}
}
