find and solve || findandsolve.com
Please wait.....

Assignment Operators in C#

In the c# programming, The Assignment Operators (=) are useful to assign a new value to the operand, and operators will work with only one operand.

In the given example, you can declare and assign a value to the variable using the assignment operator (=) like as given below.

int x;

x = 10;

If we observe the given above example, you defined a variable called "x” and assigned a new value using an assignment operator (=) based on our requirements.

In the given table lists, the different types of operators available in c# programming language assignment operators.

OperatorNameDescriptionExample(int x;)
=Equal toUsed to assign the values to variables.x= 10
+=Addition Assignment It performs the addition of left and right operands and assigns a result to the left operand.x += 10 is equal to x = x + 10
-=Subtraction Assignment It performs the subtraction of left and right operands and assigns a result to the left operand. x -= 10 equals to x = x - 10
*=Multiplication AssignmentIt performs the multiplication of left and right operands and assigns a result to the left operand.x *= 10 is equal to x= x* 10
/=Division Assignment It performs the division of left and right operands and assigns a result to the left operand. x /= 10 is equal to x= x/ 10
%=Modulo Assignment  It performs the modulo operation on two operands and assigns a result to the left operand.x %= 10 equals to x = x % 10
&=Bitwise AND AssignmentIt performs the Bitwise AND operation on two operands and assigns a result to the left operand. x&= 10 equals to x = x& 10
|=Bitwise OR AssignmentIt performs the Bitwise OR operation on two operands and assigns a result to the left operand. x |= 10 is equal to x = x | 10
^=Bitwise Exclusive OR AssignmentIt performs the Bitwise XOR operation on two operands and assigns a result to the left operand. x^= 10 is equals to x= x^ 10

>>=Right Shift AssignmentIt moves the left operand bit values to the right based on the number of positions specified by the second operand. x >>= 2 is equals to x= x >> 2

<<=Left Shift Assignment It moves the left operand bit values to the left based on the number of positions specified by the second operand. x<<= 2 is equals to x= x << 2


C# Assignment Operators Example

using System;

namespace FindAndSolve
{
    public class Program
    {
       public static void Main(string[] args)
       {
           int a = 20;
           a += 10;
           Console.WriteLine("Add Assignment: " + a);
           a *= 4;
           Console.WriteLine("Multiply Assignment: " + a);
           a %= 7;
           Console.WriteLine("Modulo Assignment: " + a);
           a &= 10;
           Console.WriteLine("Bitwise And Assignment: " + a);
           a ^= 12;
           Console.WriteLine("Bitwise XOR Assignment: " + a);
           a >>= 3;
           Console.WriteLine("Right Shift Assignment: " + a);
       }
    }
}

Output

Add Assignment: 30

Multiple Assignment: 120

Modulo Assignment: 1

Bitwise And Assignment: 0

Bitwise XOR Assignment: 12

Right Shift Assignment: 1

If we observe the given above example, you defined a variable or operand “a” and assigned new values to that variable by using assignment operators in the c#.