C# Assignment operators

C# Assignment operators

Assignment operators are used to assigning a value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of the variable on the left side otherwise the compiler will raise an error.

In the example below, we use the assignment operator (=) to assign the value 20 to a variable called x:

int x = 20;

The addition assignment operator (+=) adds a value to a variable:

int x = 20;
x += 5;

Different types of assignment operators are shown below:

  • =”(Simple Assignment): This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left.
  • +=”(Add Assignment): This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.
  • -=”(Subtract Assignment): This operator is combination of ‘-‘ and ‘=’ operators. This operator first subtracts the current value of the variable on left from the value on the right and then assigns the result to the variable on the left.
  • *=”(Multiply Assignment): This operator is combination of ‘*’ and ‘=’ operators. This operator first multiplies the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.
  • /=”(Division Assignment): This operator is combination of ‘/’ and ‘=’ operators. This operator first divides the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
  • %=”(Modulus Assignment): This operator is combination of ‘%’ and ‘=’ operators. This operator first modulo the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
  • <<=”(Left Shift Assignment) : This operator is combination of ‘<<‘ and ‘=’ operators. This operator first Left shift the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
  • >>=”(Right Shift Assignment) : This operator is combination of ‘>>’ and ‘=’ operators. This operator first Right shift the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
  • &=”(Bitwise AND Assignment): This operator is combination of ‘&’ and ‘=’ operators. This operator first “Bitwise AND” the current value of the variable on the left by the value on the right and then assigns the result to the variable on the left.
  • ^=(Bitwise Exclusive OR): This operator is combination of ‘^’ and ‘=’ operators. This operator first “Bitwise Exclusive OR” the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
  • |=”(Bitwise Inclusive OR) : This operator is combination of ‘|’ and ‘=’ operators. This operator first “Bitwise Inclusive OR” the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.

 

All assignment operators with examples:

OperatorNameExampleSame As
=Equal tox = 5x = 5
+=Addition Assignmentx += 3x = x + 3
-=Subtraction Assignmentx -= 3x = x - 3
*=Multiplication Assignmentx *= 3x = x * 3
/=Division Assignmentx /= 3x = x / 3
%=Modulo Assignmentx %= 3x = x % 3
&=Bitwise AND Assignmentx &= 3x = x & 3
|=Bitwise OR Assignmentx |= 3x = x | 3
^=Bitwise Exclusive OR Assignmentx ^= 3x = x ^ 3
>>=Right Shift Assignmentx >>= 3x = x >> 3
<<=Left Shift Assignmentx <<= 3x = x << 3

 

C# Assignment Operators Example

Following is the example of using assignment Operators in the c# programming language:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {       
        static void Main(string[] args)
        {
            // initialize variable x
            // using Simple Assignment
            // Operator "="
            int x = 20;

            // it means x = x + 10
            x += 10;
            Console.WriteLine("Add Assignment Operator: " + x);

            // initialize variable x again
            x = 30;

            // it means x = x - 8
            x -= 8;
            Console.WriteLine("Subtract Assignment Operator: " + x);

            // initialize variable x again
            x = 5;

            // it means x = x * 5
            x *= 5;
            Console.WriteLine("Multiply Assignment Operator: " + x);

            // initialize variable x again
            x = 25;

            // it means x = x / 5
            x /= 5;
            Console.WriteLine("Division Assignment Operator: " + x);

            // initialize variable x again
            x = 25;

            // it means x = x % 5
            x %= 5;
            Console.WriteLine("Modulo Assignment Operator: " + x);

            // initialize variable x again
            x = 8;

            // it means x = x << 2
            x <<= 2;
            Console.WriteLine("Left Shift Assignment Operator: " + x);

            // initialize variable x again
            x = 8;

            // it means x = x >> 2
            x >>= 2;
            Console.WriteLine("Right Shift Assignment Operator: " + x);

            // initialize variable x again
            x = 12;

            // it means x = x >> 4
            x &= 4;
            Console.WriteLine("Bitwise AND Assignment Operator: " + x);

            // initialize variable x again
            x = 12;

            // it means x = x >> 4
            x ^= 4;
            Console.WriteLine("Bitwise Exclusive OR Assignment Operator: " + x);

            // initialize variable x again
            x = 12;

            // it means x = x >> 4
            x |= 4;
            Console.WriteLine("Bitwise Inclusive OR Assignment Operator: " + x);
            Console.ReadLine();
        }       
    }
}

Output:

C# Assignment Operators Example