C# Assignment Operators


Assignment operators in C# are used to assign values to variables. They can perform operations in addition to assignment, allowing you to modify a variable's value in a single statement. Here’s a detailed explanation of the assignment operators available in C#, along with examples for each.

1. Assignment Operators Overview

OperatorDescriptionExample
=Simple assignmenta = b
+=Add and assigna += b
-=Subtract and assigna -= b
*=Multiply and assigna *= b
/=Divide and assigna /= b
%=Modulus and assigna %= b
&=Bitwise AND and assigna &= b
`=`Bitwise OR and assign
^=Bitwise XOR and assigna ^= b
<<=Left shift and assigna <<= n
>>=Right shift and assigna >>= n

2. Detailed Explanation

Simple Assignment (=)

The simple assignment operator assigns the value of the right operand to the left operand.

Example:

int a = 5; // Assigns 5 to a int b = 10; // Assigns 10 to b

Add and Assign (+=)

The add and assign operator adds the right operand to the left operand and assigns the result to the left operand.

Example:

int a = 5; a += 3; // Equivalent to a = a + 3; a now equals 8

Subtract and Assign (-=)

The subtract and assign operator subtracts the right operand from the left operand and assigns the result to the left operand.

Example:

int a = 10; a -= 4; // Equivalent to a = a - 4; a now equals 6

Multiply and Assign (*=)

The multiply and assign operator multiplies the left operand by the right operand and assigns the result to the left operand.

Example:

int a = 2; a *= 5; // Equivalent to a = a * 5; a now equals 10

Divide and Assign (/=)

The divide and assign operator divides the left operand by the right operand and assigns the result to the left operand. If the left operand is an integer, the result will also be an integer (the fractional part is discarded).

Example:

int a = 10; a /= 2; // Equivalent to a = a / 2; a now equals 5

Modulus and Assign (%=)

The modulus and assign operator calculates the remainder of the division of the left operand by the right operand and assigns the result to the left operand.

Example:

int a = 10; a %= 3; // Equivalent to a = a % 3; a now equals 1

Bitwise AND and Assign (&=)

The bitwise AND and assign operator performs a bitwise AND operation on the left and right operands and assigns the result to the left operand.

Example:

int a = 6; // Binary: 0110 int b = 3; // Binary: 0011 a &= b; // Equivalent to a = a & b; a now equals 2 (Binary: 0010)

Bitwise OR and Assign (|=)

The bitwise OR and assign operator performs a bitwise OR operation on the left and right operands and assigns the result to the left operand.

Example:

int a = 6; // Binary: 0110 int b = 3; // Binary: 0011 a |= b; // Equivalent to a = a | b; a now equals 7 (Binary: 0111)

Bitwise XOR and Assign (^=)

The bitwise XOR and assign operator performs a bitwise XOR operation on the left and right operands and assigns the result to the left operand.

Example:

int a = 6; // Binary: 0110 int b = 3; // Binary: 0011 a ^= b; // Equivalent to a = a ^ b; a now equals 5 (Binary: 0101)

Left Shift and Assign (<<=)

The left shift and assign operator shifts the bits of the left operand to the left by the number of positions specified by the right operand and assigns the result to the left operand.

Example:

int a = 5; // Binary: 0101 a <<= 1; // Equivalent to a = a << 1; a now equals 10 (Binary: 1010)

Right Shift and Assign (>>=)

The right shift and assign operator shifts the bits of the left operand to the right by the number of positions specified by the right operand and assigns the result to the left operand.

Example:

int a = 10; // Binary: 1010 a >>= 1; // Equivalent to a = a >> 1; a now equals 5 (Binary: 0101)

3. Using Assignment Operators in C#

Assignment operators simplify code by allowing you to perform an operation and assignment in a single step. Here’s an example demonstrating how to use various assignment operators:

using System; class Program { static void Main() { int a = 10; int b = 5; a += b; // a = 15 Console.WriteLine($"a += b: {a}"); a -= 3; // a = 12 Console.WriteLine($"a -= 3: {a}"); a *= 2; // a = 24 Console.WriteLine($"a *= 2: {a}"); a /= 4; // a = 6 Console.WriteLine($"a /= 4: {a}"); a %= 5; // a = 1 Console.WriteLine($"a %= 5: {a}"); int c = 6; // Binary: 0110 int d = 3; // Binary: 0011 c &= d; // c = 2 (Binary: 0010) Console.WriteLine($"c &= d: {c}"); c |= d; // c = 3 (Binary: 0011) Console.WriteLine($"c |= d: {c}"); c ^= d; // c = 0 (Binary: 0000) Console.WriteLine($"c ^= d: {c}"); int e = 5; // Binary: 0101 e <<= 1; // e = 10 (Binary: 1010) Console.WriteLine($"e <<= 1: {e}"); e >>= 1; // e = 5 (Binary: 0101) Console.WriteLine($"e >>= 1: {e}"); } }

4. Summary

  • Assignment operators in C# allow you to assign values to variables, with several operators combining assignment with arithmetic and bitwise operations.
  • The main assignment operators include =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, and >>=.
  • These operators help simplify code and make it more readable by reducing the number of lines needed to perform operations and assignments.
  • Understanding how to use assignment operators effectively is crucial for writing concise and efficient C# programs.