C# Assignment Operators with Examples

  Keywords : C# having different type of assignment operators those are simple assignment operators and compound assignment operators with examples

Here we will learn what are the assignment operators in c# with examples, different type of assignment operators (equal to, addition, subtraction, multiplication, division, modulo, right shift, left shift, etc.) in c# with examples and how to use simple and compound assignment operators in c# to assign a new values to the operands with examples.

C# Assignment Operators

In c#, Assignment Operators are used to assign a new value to the operand and these operators will work with only one operand.

 

For example, we can declare and assign a value to the variable using assignment operator (=) like as shown below.

 

int a;

a = 10;

If you observe above sample we defined a variable called “a” and assigned a new value using assignment operator (=) based on our requirements.

 

Following table lists the different type of operators available in c# assignment operators.

 

OperatorNameDescriptionExample
= Equal to It is used to assign the values to variables. int a; a = 10
+= Addition Assignment It perform an addition of left and right operands and assign a result to the left operand. a += 10 is equals to a = a + 10
-= Subtraction Assignment It perform a subtraction of left and right operands and assign a result to the left operand. a -= 10 is equals to a = a - 10
*= Multiplication Assignment It perform a multiplication of left and right operands and assign a result to the left operand. a *= 10 is equals to a = a * 10
/= Division Assignment It perform a divison of left and right operands and assign a result to the left operand. a /= 10 is equals to a = a / 10
%= Modulo Assignment It perform a modulo operation on two operands and assign a result to the left operand. a %= 10 is equals to a = a % 10
&= Bitwise AND Assignment It perform a Bitwise AND operation on two operands and assign a result to the left operand. a &= 10 is equals to a = a & 10
|= Bitwise OR Assignment It perform a Bitwise OR operation on two operands and assign a result to the left operand. a |= 10 is equals to a = a | 10
^= Bitwise Exclusive OR Assignment It perform a Bitwise XOR operation on two operands and assign a result to the left operand. a ^= 10 is equals to a = a ^ 10
>>= Right Shift Assignment It moves the left operand bit values to right based on the number of positions specified by second operand. a >>= 2 is equals to a = a >> 2
<<= Left Shift Assignment It moves the left operand bit values to left based on the number of positions specified by second operand. a <<= 2 is equals to a = a << 2

C# Assignment Operators Example

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

 

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            int x = 20;

            x += 10;

            Console.WriteLine("Add Assignment: " + x);

            x *= 4;

            Console.WriteLine("Multiply Assignment: " + x);

            x %= 7;

            Console.WriteLine("Modulo Assignment: " + x);

            x &= 10;

            Console.WriteLine("Bitwise And Assignment: " + x);

            x ^= 12;

            Console.WriteLine("Bitwise XOR Assignment: " + x);

            x >>= 3;

            Console.WriteLine("Right Shift Assignment: " + x);

            Console.WriteLine("Press Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we defined a variable or operand “x” and assigning a new values to that variable by using assignment operators in c# programming language.

Output of C# Assignment Operators Example

When we execute above c# program, we will get the result like as shown below.

 

C#Assigenment Operator Example Result

 

 This is how we can use assignment operators in c# to assign a new values to the variable based on our requirements.