Assignment and Compound Assignment Operators in C# Programming


Introduction

Assignment operators are used to assign values to variables in C#. Compound assignment operators combine an operation with assignment, making the code more concise. This tutorial covers their usage with examples.

Assignment Operator

The basic assignment operator is =. It assigns the value on the right-hand side to the variable on the left-hand side.

Example: Assignment Operator

    using System;
   class Program
    {
        static void Main()
        {
            int number;
            number = 10; // Assigning value 10 to the variable 'number'
           Console.WriteLine("The value of number is: " + number);
        }
    }
        

Output:

    The value of number is: 10
        

Compound Assignment Operators

Compound assignment operators perform an operation and assign the result to the variable in a single step.

  • +=: 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

Example: Compound Assignment Operators

    using System;
   class Program
    {
        static void Main()
        {
            int number = 10;
           number += 5; // Equivalent to: number = number + 5
            Console.WriteLine("After += operation: " + number);
           number -= 3; // Equivalent to: number = number - 3
            Console.WriteLine("After -= operation: " + number);
           number *= 2; // Equivalent to: number = number * 2
            Console.WriteLine("After *= operation: " + number);
           number /= 4; // Equivalent to: number = number / 4
            Console.WriteLine("After /= operation: " + number);
           number %= 3; // Equivalent to: number = number % 3
            Console.WriteLine("After %= operation: " + number);
        }
    }
        

Output:

    After += operation: 15
    After -= operation: 12
    After *= operation: 24
    After /= operation: 6
    After %= operation: 0
        

Example: Bitwise Compound Assignment

    using System;
   class Program
    {
        static void Main()
        {
            int number = 5; // Binary: 0101
           number &= 3; // Equivalent to: number = number & 3 (Binary AND)
            Console.WriteLine("After &= operation: " + number); // Binary: 0001
           number |= 2; // Equivalent to: number = number | 2 (Binary OR)
            Console.WriteLine("After |= operation: " + number); // Binary: 0011
           number ^= 1; // Equivalent to: number = number ^ 1 (Binary XOR)
            Console.WriteLine("After ^= operation: " + number); // Binary: 0010
           number <<= 1; // Equivalent to: number = number << 1 (Left shift)
            Console.WriteLine("After <<= operation: " + number); // Binary: 0100
           number >>= 2; // Equivalent to: number = number >> 2 (Right shift)
            Console.WriteLine("After >>= operation: " + number); // Binary: 0001
        }
    }
        

Output:

    After &= operation: 1
    After |= operation: 3
    After ^= operation: 2
    After <<= operation: 4
    After >>= operation: 1
        

Conclusion

Assignment and compound assignment operators are powerful tools in C# that simplify code by combining operations and assignments. Understanding these operators allows you to write concise and efficient code.




Advertisement