C# - Assignment Operators



What Are Assignment Operators in C#?

C# assignment operators assign values to variables along with the assignment; in some cases, these operators perform mathematical operators.

List of C# Assignment Operators

There are following assignment operators supported by C# −

Operator Symbol Description Example
Simple Assignment = Simple assignment operator, Assigns values from right side operands to left side operand C = A + B assigns value of A + B into C
Add and Assign += Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A
Subtract and Assign -= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand C -= A is equivalent to C = C - A
Multiply and Assign *= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand C *= A is equivalent to C = C * A
Divide and Assign /= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand C /= A is equivalent to C = C / A
Modulus and Assign %= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand C %= A is equivalent to C = C % A
Left Shift and Assign <<= Left shift AND assignment operator C <<= 2 is same as C = C << 2
Right Shift and Assign >>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2
Bitwise AND and Assign &= Bitwise AND assignment operator C &= 2 is same as C = C & 2
Bitwise XOR and Assign ^= bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^ 2
Bitwise OR and Assign |= bitwise inclusive OR and assignment operator C |= 2 is same as C = C | 2

Example of Assignment Operators

The following example demonstrates all the assignment operators available in C# −

using System;

namespace OperatorsAppl {

   class Program {
   
      static void Main(string[] args) {
         int a = 21;
         int c;
         c = a;
         Console.WriteLine("Line 1 - =  Value of c = {0}", c);
         
         c += a;
         Console.WriteLine("Line 2 - += Value of c = {0}", c);
         
         c -= a;
         Console.WriteLine("Line 3 - -=  Value of c = {0}", c);
         
         c *= a;
         Console.WriteLine("Line 4 - *=  Value of c = {0}", c);
         
         c /= a;
         Console.WriteLine("Line 5 - /=  Value of c = {0}", c);
         
         c = 200;
         c %= a;
         Console.WriteLine("Line 6 - %=  Value of c = {0}", c);
         
         c <<= 2;
         Console.WriteLine("Line 7 - <<=  Value of c = {0}", c);
         
         c >>= 2;
         Console.WriteLine("Line 8 - >>=  Value of c = {0}", c);
         
         c &= 2;
         Console.WriteLine("Line 9 - &=  Value of c = {0}", c);
         
         c ^= 2;
         Console.WriteLine("Line 10 - ^=  Value of c = {0}", c);
         
         c |= 2;
         Console.WriteLine("Line 11 - |=  Value of c = {0}", c);
         Console.ReadLine();
      }
   }
}

When the above code is compiled and executed, it produces the following result −

Line 1 - = Value of c = 21
Line 2 - += Value of c = 42
Line 3 - -= Value of c = 21
Line 4 - *= Value of c = 441
Line 5 - /= Value of c = 21
Line 6 - %= Value of c = 11
Line 7 - <<= Value of c = 44
Line 8 - >>= Value of c = 11
Line 9 - &= Value of c = 2
Line 10 - ^= Value of c = 0
Line 11 - |= Value of c = 2

More Examples on C# Assignment Operators

Example 1: Basic Assignment (=)

In this example, we assign a value to a variable and print it.

using System;

class Program
{
    static void Main()
    {
        int x = 10; // Assigning value 10 to x
        Console.WriteLine("x: " + x);
    }
}

When the above code is compiled and executed, it produces the following result −

x: 10

Example 2: Using += (Addition and Assignment)

In this example, we add and assign a value using +=.

using System;

class Program
{
    static void Main()
    {
        int x = 5;
        x += 3; // Equivalent to x = x + 3

        Console.WriteLine("x after += : " + x);
    }
}

When the above code is compiled and executed, it produces the following result −

x after += : 8

Example 3: Using -= (Subtraction and Assignment)

In this example, we subtract and assign a value using -=.

using System;

class Program
{
    static void Main()
    {
        int x = 10;
        x -= 4; // Equivalent to x = x - 4

        Console.WriteLine("x after -= : " + x);
    }
}

When the above code is compiled and executed, it produces the following result −

x after -= : 6

Example 4: Using *= (Multiplication and Assignment)

In this example, we multiply and assign a value using *=.

using System;

class Program
{
    static void Main()
    {
        int x = 5;
        x *= 2; // Equivalent to x = x * 2

        Console.WriteLine("x after *= : " + x);
    }
}

When the above code is compiled and executed, it produces the following result −

x after *= : 10

Example 5: Using /= (Division and Assignment)

In this example, we divide and assign a value using /=.

using System;

class Program
{
    static void Main()
    {
        int x = 20;
        x /= 4; // Equivalent to x = x / 4

        Console.WriteLine("x after /= : " + x);
    }
}

When the above code is compiled and executed, it produces the following result −

x after /= : 5

Example 6: Using %= (Modulus and Assignment)

In this example, we use modulus and assign a value using %=.

using System;

class Program
{
    static void Main()
    {
        int x = 10;
        x %= 3; // Equivalent to x = x % 3

        Console.WriteLine("x after %= : " + x);
    }
}

When the above code is compiled and executed, it produces the following result −

x after %= : 1

Real-World Use Cases for Assignment Operators

Example 7: Accumulating Scores in a Game

In this example, we add points to a player's score.

using System;

class Program
{
    static void Main()
    {
        int score = 50;
        score += 10; // Add points

        Console.WriteLine("Current Score: " + score);
    }
}

When the above code is compiled and executed, it produces the following result −

Current Score: 60

Example 8: Decreasing Inventory Stock in an E-Commerce System

In this example, we decrease stock when items are sold.

using System;

class Program
{
    static void Main()
    {
        int stock = 100;
        stock -= 2; // Two items sold

        Console.WriteLine("Stock Remaining: " + stock);
    }
}

When the above code is compiled and executed, it produces the following result −

Stock Remaining: 98

Example 9: Doubling Investment Amount

In this example, we double an investment amount.

using System;

class Program
{
    static void Main()
    {
        double investment = 1000;
        investment *= 2; // Double the amount

        Console.WriteLine("Total Investment: " + investment);
    }
}

When the above code is compiled and executed, it produces the following result −

Total Investment: 2000

Best Practices

The following are the best practices while using assignment operators:

  • You should use += and -= for counters instead of x = x + 1.
  • You must check for division by zero before using /=.
  • You should use assignment operators to keep your code clean and readable.
  • You must understand operator precedence when combining them in expressions.
Advertisements